簡體   English   中英

c ++類的重載賦值運算符,帶有指向其他類的指針

[英]c++ Overload Assignment operator of class with pointers to other class

我有一個包含指針的 N​​etwork 類。 我想為它重載賦值運算符。

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

};

要用向量替換所有指針,我必須進行哪些更改/添加>

步驟1。 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 替換。 注意順序,因為這很重要。 在定義向量之前,必須完全定義向量包含的類。 前向聲明在這里還不夠好。

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現在是向量,向量知道它們的長度。

接下來,使用多種不同的方式將項目放置、復制到向量中(請參閱文檔)。 通常使用 place_back 方法,將元素一一添加。 在網絡的情況下,向量中的層數是已知的,所以有一個稍微快一點的選擇:

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 Layer分配空間。 net_layers(tot_layers)做同樣的事情net_layers

旁白: net_layers可能不應該在Network 這是Layer一個屬性, Layer應該跟蹤它。 (它已經這樣做了,因為向量Neurons知道它的長度)。 我建議

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM