如何将“= default”与具有主体的构造函数一起使用?

How to use " = default" with a constructor that has a body?

假设我有一个 class,它有几个成员变量。例如,在复制构造函数中,我想复制所有成员并做更多工作。不是显式复制成员变量,而是如何告诉编译器具有默认行为(复制成员变量)并执行函数主体中的操作?

像这样:

class X
{
public:

    // This constructor should copy all the members
    // and also do what's inside the constructor's body.   
    X(const X& x) = default
    {
        // Do some work.
    }
};

从构造的角度来看,这没有多大意义,因为构造函数的工作是设置成员数据,没有别的。

但是如果您需要这种模式,例如以某种方式注册对象,一个解决方案是

class Y : public X
{
    Y(const Y&){
        // Do some work
    }
    // No member data here 
};