我的问题和其他问题非常相似,但有点不同。我有一个示例代码:
class B1 {
private :
int * ptr;
public:
B1() : a{ 1 } { ptr = new int{ 2 }; }
B1(const B1& other) : a{ other.a } { ptr = new int{ *other.ptr }; }
~B1() { delete ptr; }
int a;
virtual void smthing() = 0;
};
class D : B1 {
public:
D(int i) : B1{} {}
D(const D& a) : B1{ a } {}
void smthing() { return; };
};
int main() {
D d { 3 };
D dd { d };
return 0;
}
我正在使用vs2015,这个代码是有效的,但是给了我错误:抽象类类型的对象" B1"不被允许...
如果我删除这一行D(const D& a) : B1{ a } {}
,则调用基类复制构造函数并且没有问题,但是如果我需要派生类复制构造函数,我怎样才能使这个工作没有错误?
感谢您的回答!