如果我有一个抽象类和派生类,并且不使用STL数据结构类,如何通过多态方式创建动态对象数组?(例如向量、列表等)?
静态对象数组
静态对象数组
TwoDimensionShape *shapes[2];
shapes[0] = &Triangle("right", 8.0, 12.0);
shapes[1] = &Rectangle(10);
我知道我不能这样做,因为你不能创建抽象类的实例:
cin >> x;
TwoDimensionShape *s = new TwoDimensionShape [x];
编辑:
感谢Nick,这个可以工作:
int x = 5;
TwoDimensionShape **shapes = new (TwoDimensionShape*[x]);
TwoDimensionShape **shapes = new (TwoDimensionShape*)[x];
- imreal