简体   繁体   中英

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

I have a Network class which contains pointers. I want to overload assignment operator for it.

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

Constructor

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

How to overload assignment operator for it correctly with deep copy?

Please help, want to replace all pointers with vectors...

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

};

To replace all pointers with vectors, what changes/additions I have to make>

Step one. Replace the dynamic arrays with std::vector s. All done.

Can't do that? Cannibalize your constructor and rather than setting the member variables to the input parameters, set the members equal to the source Network.

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

Step 1 redux:

All of the dynamic arrays have been replaced by std::vector. Note the ordering because this is important. A class contained by a vector must be fully defined before defining the vector. Forward declaration is not good enough here.

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

Note: unsigned long net_tot_layers; has been removed because it is no longer necessary. Layers and net_layers are now vectors and vectors know their length.

Next, items are placed, copied, into a vector using a number of different ways ( See documentation ). Usually the place_back method is used, adding elements one by one. In the network case, the number of Layers in the vector is known, so there is a slightly faster option:

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

The bit after the colon is a member initializer list. This is a really cool C++ feature I wish they taught in school more often. It allows you to initialize members of the class before the constructor body is run. This makes sure that all of the pieces are in place before they are needed and often has some performance advantages. Layers(tot_layers) calls the vector constructor and tells it to allocate space for tot_layers Layer s. net_layers(tot_layers) does the same thing for net_layers .

An aside: net_layers probably shouldn't be in Network . This is a property of Layer , and Layer should keep track of it. (It already does because vector Neurons knows it's length). I recommend

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM