1

I have a class looking like

class A{
    double a, b, c, d, e;
    float af, bf, cf, df, ef;
    std::vector<double> av, bv, cv, dv, ev;
    std::vector<std::vector<double>> avv, bvv, cvv, dvv, evv;

    A(){}
    A(/*init values*/){/*Initialize all the values above using the input values*/}
    ~A(){}
}

Now I would like to implement an assignment operator, such that I can do (in a second class):

class B{
    private:
        A ac, bc, dc;
    public:
        B(/*init values*/)
        {
            ac = A(/*init values*/);
            bc = A(/*init values*/);
            dc = A(/*init values*/);
        }
        ~B(){}
}

I know that I can follow What is the copy-and-swap idiom? and What are the basic rules and idioms for operator overloading? and implement

A& A::operator=(A rhs)
{
  swap(rhs);
  return *this;
}

and the respective swap-function:

friend void swap(A& first, A& second)
{
    using std::swap;

    swap(/*for each element*/);
}

Doing so for all the elements is prone for forgetting one element, resulting in errors in the code. The same goes for extensions in the future, i.e. adding variables in the class header, but not in the swap function. Is there an easier way for doing that?

arc_lupus
  • 3,942
  • 5
  • 45
  • 81

1 Answers1

0

You could put all data member's in an aggregate, and just do not declare any constructor, assignment operator and destructor. You will get the benefits of the default move/copy constructor/assignment and aggregate initialization (which may help you if initialization is straight forward):

struct A{
    double a, b, c, d, e;
    float af, bf, cf, df, ef;
    std::vector<double> av, bv, cv, dv, ev;
    std::vector<std::vector<double>> avv, bvv, cvv, dvv, evv;
    };
void swap(A& a,A& b){
    a=std::exchange(b,a);
    }

If you have to maintain some invariants, it may be helpfull to declare the data in an inner structure:

class A{
  struct data_t{
    double a, b, c, d, e;
    float af, bf, cf, df, ef;
    std::vector<double> av, bv, cv, dv, ev;
    std::vector<std::vector<double>> avv, bvv, cvv, dvv, evv;
    };
  data_t data;
  //[...]
  void swap(A& other){
     data = std::exchange(other.data,data);
     }
  };
Oliv
  • 17,610
  • 1
  • 29
  • 72
  • How does this version differs from the version suggested in the comments above (just curious)? – arc_lupus Dec 19 '17 at 18:51
  • @arc_lupus It is worst than I thought. You should consider to buy an academic book about C++, you belong to the categorie of poeple who earns the largest benefit from such lecture. – Oliv Dec 20 '17 at 09:37
  • Do my questions look so awful? o_O – arc_lupus Dec 20 '17 at 09:38
  • @arc_lupus, Yes it is. Moreover, your question and comments I read show that you are realy lacking a lot of fundamental concepts about c++ classes. Answering you would be a lost of time for you and me. You should target to know every thing that is below the "classes" item: http://en.cppreference.com/w/cpp/language. You should start right now to accept that we all are eternal apprentice. Refusing it, living with the erroneous belief you know, will make you an eternal ignorant. What could you learn if you already know? – Oliv Dec 20 '17 at 09:48
  • Oh well, I thought I understood at least basic concepts until now. Back to the basics, it is. Thank you for the feedback! – arc_lupus Dec 20 '17 at 09:49