我有一个类B
,它拥有一组构造函数和一个赋值运算符。
这是它的定义:
class B
{
public:
B();
B(const string& s);
B(const B& b) { (*this) = b; }
B& operator=(const B & b);
private:
virtual void foo();
// and other private member variables and functions
};
我想创建一个继承类 D
,只需重写函数foo()
,不需要进行其他更改。
但是,我希望D
具有与B
相同的构造函数集,包括复制构造函数和赋值运算符:
D(const D& d) { (*this) = d; }
D& operator=(const D& d);
我是否必须用 D
重写它们所有,或者有没有一种方法可以使用 B
的构造函数和运算符?我特别想避免重写赋值运算符,因为它必须访问 B
的所有私有成员变量。
foo
方法,可以使用using B::operator=;
来继承赋值运算符,但是复制和移动构造函数无法被继承:https://dev59.com/hqnka4cB1Zd3GeqPOHHM - anton_rh