我有一个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
所有的编译都很顺利,但似乎我的多态有些问题?我感到困惑。
std::vector<B>
)。 - CB Bailey