【问题标题】:c++ Overload Assignment operator of class with pointers to other classc++ 具有指向其他类的指针的类的重载赋值运算符
【发布时间】:2015-11-19 07:05:58
【问题描述】:

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

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

};

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

【问题讨论】:

  • 使用矢量,您将节省大量时间。
  • 谢谢,会试试的。还有一些问题。

标签: c++ operator-overloading assignment-operator


【解决方案1】:

第一步。用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 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方法,一个一个地添加元素。在网络的情况下,向量中的Layer个数是已知的,所以有一个稍微快一点的选择:

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

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

顺便说一句:net_layers 可能不应该在Network 中。这是Layer 的属性,Layer 应该跟踪它。 (它已经这样做了,因为向量 Neurons 知道它的长度)。我推荐

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

【讨论】:

  • 感谢您的及时回复。
  • 还有更多问题
猜你喜欢
  • 1970-01-01
  • 2013-02-03
  • 1970-01-01
  • 1970-01-01
  • 2018-10-17
  • 2014-05-18
  • 2021-05-13
  • 2020-08-17
相关资源
最近更新 更多