mardi 8 mai 2018

C++11: Copy assignment with const fields

Suppose I have a class

struct A {

     const vector<double> v;

     A( const vector<double> & v ) : v(v) {
     }
};

Now I want to enable copy assignment for this class. How would I do that?

The typical way that I have seen copy assignment implemented is with something like this:

struct A {

     const vector<double> v;

     A( const vector<double> & v ) : v(v) {
     }

     A &operator=(const A &a) {
         v = a.v;
         return *this;
     }

};

But this will not work for the class at hand because the field v is const.

Aucun commentaire:

Enregistrer un commentaire