Class 复制构造函数将指针复制到抽象类
假设我有一个类General,它持有一个指向抽象类*\u abstract的指针。 如果我想实现通用复制构造函数,它是如何实现的 我尝试了一下,但失败了:Class 复制构造函数将指针复制到抽象类,class,pointers,constructor,copy,abstract,Class,Pointers,Constructor,Copy,Abstract,假设我有一个类General,它持有一个指向抽象类*\u abstract的指针。 如果我想实现通用复制构造函数,它是如何实现的 我尝试了一下,但失败了: General::General(const General &other) { *_abstract = *other._abstract; } 我还尝试: General::General(const General &other) { *_abstract = new Abstract(); *
General::General(const General &other)
{
*_abstract = *other._abstract;
}
我还尝试:
General::General(const General &other)
{
*_abstract = new Abstract();
*_abstract = *other._abstract;
}
这是不可能的,因为抽象类初始化(没有构造函数)如果需要深度复制抽象类,则向抽象类添加一个虚拟克隆函数,并使用该函数进行复制
我找到了这个链接,它提供了您所说内容的示例,谢谢!