class 的 c++ 重载赋值运算符,带有指向其他 class 的指针

c++ Overload Assignment operator of class with pointers to other class

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

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;
}

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

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

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

};

要用向量替换所有指针,changes/additions 我必须做的事情>

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

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

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。请注意顺序,因为这很重要。向量包含的 class 必须在定义向量之前完全定义。前向声明在这里不够好。

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 现在是向量,向量知道它们的长度。

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

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

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

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

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