<分区>
我有一个 A 类:
class A
{
public:
virtual double getValue() = 0;
}
还有一个 B 类:
class B : public A
{
public:
virtual double getValue() { return 0.0; }
}
然后在 main() 中我做:
A * var;
var = new B[100];
std::cout << var[0].getValue(); //This works fine
std::cout << var[1].getValue(); //This, or any other index besides 0, causes the program to quit
如果我这样做:
B * var;
var = new B[100];
std::cout << var[0].getValue(); //This works fine
std::cout << var[1].getValue(); //Everything else works fine too
一切都可以正常编译,但似乎我的多态性似乎有问题?我很困惑。