且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何扩展一个编译器生成的复制构造函数

更新时间:2022-05-11 21:42:02

不幸的是,这意味着你必须自己做所有的腿工作!您可以将成员分组到类中的某种 impl _ 结构,然后依靠复制ctor。

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.

例如:

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

  impl_ data;
};

现在在您的副本中

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