【发布时间】:2013-05-02 18:21:44
【问题描述】:
Shape *shape[100];//global scope
Square sqr;//global scope
void inputdata() {
int len,width;
cout << "enter length";
cin >> len;
cout << "enter width";
cin >> width;
Square sqr(len,width);
shape[0] = &sqr;
//----> if shape[0]->computeArea(); here works fine.
}
void computeArea() {
shape[0]->computeArea(); // --> run fail error
}
Shape 是父类,square 是子类。两者都有 computeArea();
当代码到达 computeArea() 时,我遇到了一个奇怪的运行失败错误。该程序只是终止而没有给我任何错误让我找到并修复它...它只是显示运行失败并停止程序。
如果代码在 inputdata() 内,程序能够正常运行并显示 ->computeArea() 但是当我将它分开时,它只是无法正常运行。有什么解决办法吗?
【问题讨论】:
-
从你发布的代码来看,我看不出问题,你可能在其他地方有问题。
标签: c++ object inheritance polymorphism virtual