c++使用指向其他类的指针重载类的赋值运算符

标签 c++ operator-overloading assignment-operator

我有一个包含指针的网络类。我想为它重载赋值运算符。

class Network
{
public:
    Network();

    Layer *Layers;      //The total layers in network
    unsigned long net_tot_layers;   //Number of layers
    unsigned long *net_layers;      //Array which tells no. of neurons in each layer
    Network::Network(double learning_rate, unsigned long layers[], unsigned long tot_layers);
};

构造函数

Network::Network(double learning_rate, unsigned long layers[], unsigned long tot_layers) {
    net_layers = new unsigned long[tot_layers]; //Initialize the layers array
    Layers = new Layer[tot_layers];
    for (unsigned i = 0; i < tot_layers; i++) {
        net_layers[i] = layers[i];
        Layers[i].Initialize(layers[i]); //Initialize each layer with the specified size
    }

    net_tot_layers = tot_layers;
}

如何使用深拷贝正确地为它重载赋值运算符?

请帮忙,想用 vector 替换所有指针...

class Layer
{
public:
    Layer();
    ~Layer();
    Neuron *Neurons;
    void Initialize(unsigned long size);    
};

class Neuron
{
public:
    Neuron();   // Constructor
    ~Neuron();  // Destructor

    Link* Links;    //Links
    Neuron();   // Constructor
};
class Link {
public:
    Link(double weight = 0.0); // Constructor
    ~Link();    // Distructor
    double weight;  //Weight of the link

};

要用 vector 替换所有指针,我必须进行哪些更改/添加>

最佳答案

第一步。用 std::vector 替换动态数组。全部完成。

做不到吗?蚕食您的构造函数,而不是将成员变量设置为输入参数,而是将成员设置为等于源网络。

Network & Network::operator=(const Network & src) {
    net_tot_layers = src.net_tot_layers;

    // make new arrays
    net_layers = new unsigned long[net_tot_layers]; 
    Layers = new Layer[net_tot_layers];

    // copy from source Network to this network
    for (unsigned i = 0; i < net_tot_layers ; i++) {
        net_layers[i] = src.net_layers[i];
        Layers[i] = src.Layers[i]; // and make damn sure Layer also has an operator=
    }
    return *this;
}

第 1 步还原:

所有的动态数组都被 std::vector 取代了。请注意顺序,因为这很重要。在定义 vector 之前,必须完全定义 vector 包含的类。前向声明在这里不够好。

class Link {
public:
    Link(double weight = 0.0); // Constructor
    ~Link();    // Distructor
    double weight;  //Weight of the link

};

class Neuron
{
public:
    Neuron();   // Constructor
    ~Neuron();  // Destructor

    std::vector<Link> Links;
    Neuron();   // Constructor
};

class Layer
{
public:
    Layer();
    ~Layer();
    std::vector<Neuron> Neurons;
    void Initialize(unsigned long size);    
};

class Network
{
public:
    Network();

    std::vector<Layer> Layers;      //The total layers in network
    std::vector<unsigned long> net_layers; //Array which tells no. of neurons in each layer

    Network::Network(double learning_rate, unsigned long layers[], unsigned long tot_layers);
};

注意:unsigned long net_tot_layers; 已被删除,因为不再需要它。 Layersnet_layers 现在是 vector , vector 知道它们的长度。

接下来,使用多种不同的方式(See documentation)将项目放置、复制到 vector 中。通常使用place_back方法,一个一个添加元素。在网络情况下, vector 中的层数是已知的,因此有一个稍微快一点的选项:

Network::Network(double learning_rate, 
                 unsigned long layers[], 
                 unsigned long tot_layers): Layers(tot_layers),
                                            net_layers(tot_layers){

冒号后的位是成员初始化列表。这是一个非常酷的 C++ 特性,我希望他们能在学校更频繁地教授。它允许您在构造函数主体运行之前初始化类的成员。这可确保所有部件在需要之前就位,并且通常具有一些性能优势。 Layers(tot_layers) 调用 vector 构造函数并告诉它为 tot_layers Layer 分配空间。 net_layers(tot_layers)net_layers 做同样的事情。

旁白:net_layers 可能不应该在 Network 中。这是 Layer 的一个属性,Layer 应该跟踪它。 (它已经这样做了,因为 vector Neurons 知道它的长度)。我推荐

unsigned long Layer::GetNumNeurons()
{
    return Neurons.size();
}

关于c++使用指向其他类的指针重载类的赋值运算符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33797274/

相关文章:

c++ - 二维数组数据成员的初始化

c++ - 当基于 CDialog 的应用程序启动时,如何将我的辅助对话窗口置于顶部?

c++ - 搜索空字符串时 find vs find_first_of

c# - C# 中的重载运算符实际上是好的做法吗?

c++ - 调用子插入运算符

c++ - 为简单结构定义哪个复制/move 构造函数/运算符?

C++检查一个 vector 中的值是否存在于另一个 vector 中

c++ - bool 条件赋值

c++ - 为什么我的重载 << 不返回任何数据?

dictionary - Go 中的哪些语义规则决定何时发生单值赋值与何时发生二值赋值?