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

时间:2015-11-19 07:05:58

标签: 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;
}

如何使用深层复制正确地重载赋值运算符?

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

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

};

要用向量替换所有指针,我必须做出哪些更改/添加&gt;

1 个答案:

答案 0 :(得分:1)

第一步。用@Transactional替换动态数组。一切都完成了。

不能这样做吗?对你的构造函数进行自动化,而不是将成员变量设置为输入参数,将成员设置为等于源网络。

std::vector

第1步redux:

所有动态数组都已被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;
}

注意: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;Layers现在是向量和向量知道它们的长度。

接下来,使用多种不同的方式(See documentation)将项目放置,复制到矢量中。通常使用place_back方法,逐个添加元素。在网络案例中,向量中的层数是已知的,因此选项稍快一些:

net_layers

冒号后面的位是成员初始化列表。这是一个非常酷的C ++功能,我希望他们更频繁地在学校教授。它允许您在运行构造函数体之前初始化类的成员。这确保了所有部件在需要之前就位,并且通常具有一些性能优势。 Network::Network(double learning_rate, unsigned long layers[], unsigned long tot_layers): Layers(tot_layers), net_layers(tot_layers){ 调用向量构造函数并告诉它为Layers(tot_layers) tot_layers s分配空间。 Layernet_layers(tot_layers)执行相同的操作。

旁白:net_layers可能不应该在net_layers。这是Network的属性,Layer应该跟踪它。 (它已经成功,因为向量Layer知道它的长度)。我推荐

Neurons