很抱歉混淆了这个话题,这是最初的问题:
如何重新分配指向基类的指针数组,实际上指向不同的派生类?
如果可能的话,我不应该在这里使用动态转换、typeid 或 RTTI ..
编辑我刚刚意识到我可以尝试保存数组元素并将新数组中的指针设置为旧元素.. 但是如何操作 operator= 或 Cctor?
或者:
如何通过实际复制元素来重新分配这个数组“不好的方式”?
一个例子:
class Base {...}
class Derived1 : public Base {...}
class Derived2 : public Base {...}
int main()
{
int arrayLength=0, arrayMaxLength=3;
Base **array=new Base*[arrayMaxlength];
array[0]=new Derived1();
array[1]=new Derived2();
//Reallocation starts...
Base **tmp=new Base*[arrayMaxLength*=2];
for(int i=0;i<arrayLength;i++)
tmp[i]=new Base(*array[i]); //<------ What to put here instead of Base?
//The unimportant rest of Reallocation..
for(int i=0;i<arrayLength;i++)
delete array[i];
delete [] array;
array=tmp;
}