【发布时间】:2010-11-24 12:21:45
【问题描述】:
我想要一个带有指向对象的指针的向量的深层副本,但对象可以是 C 或 B。我知道令人困惑(我解释它的方式),让我举例说明。
class A {
A(const A& copyme) { }
void UnableToInstantiateMeBecauseOf() =0;
};
class B {
B(const B& copyme) : A(copyme) {}
};
class C {
C(const C& copyme) : A(copyme) {}
};
std::vector<A*>* CreateDeepCopy(std::vector<A*>& list)
{
std::vector<A*>* outList = new std::vector<A*>();
for (std::vector<A*>::iterator it = list.begin(); it != list.end(); ++it)
{
A* current = *it;
// I want an copy of A, but it really is either an B or an C
A* copy = magic with current;
outList->push_back(copy);
}
return outList;
}
如何创建一个你不知道它是什么继承类型的对象的副本?
【问题讨论】:
标签: c++ multiple-inheritance copy-constructor