How can I extend a compiler generated copy constructor

后端 未结 5 819
梦如初夏
梦如初夏 2020-12-17 09:41

I frequently run into the problem, that I must extend a compiler generated copy constructor. Example:

class xyz;
class C
{
    ...
    int a, b, c; 
    std:         


        
5条回答
  •  不知归路
    2020-12-17 10:13

    the moment you define your own copy ctor, the compiler does not bother generating one for you. Unfortunately this means you have to do all the leg work yourself! You could group the members into some sort of impl_ structure within your class, and then rely on the copy ctor for that.

    for example:

    class xyz;
    class C
    {
      struct impl_
      {
        int a, b, c; 
        std::set mySet;
        xyz *some_private_ptr;
      };
    
      impl_ data;
    };
    

    now in your copy ctor

    C::C(const C &other) : data(other.data)
    {
     // specific stuff...      
    }
    

提交回复
热议问题