class a //my base abstract class
{
public:
virtual void foo() = 0;
};
class b : public a //my child class with new member object
{
public:
void foo()
{}
int obj;
};
int main()
{
b bee;
a * ptr = &bee;
ptr->obj; //ERROR: class a has no member named "obj"
}
我的问题是,当我有一个指向基类(“a”)的指针指向子类(“b”)对象时,如何访问“obj”成员?我知道铸造应该可以解决问题,但我正在寻找更好的解决方案。