#include<iostream>
class A {
private :
int _a;
public :
A(int i=0) : _a(i){}
virtual void setA(int i) { _a = i;}
virtual int getA(){ return _a;}
};
class B : public A
{
private :
int _b;
public :
B(int i) : _b(i){}
virtual void setB(int i) { _b = i;}
virtual int getB(){ return _b;}
};
int main(){
A* a1 = new B(10);
std::cout << a1->getA() << "\t " << a1->getB() << std::endl;
return 0;
}
编译时出现以下错误:
test.cpp(28): error C2039: 'getB' : is not a member of 'A'
不应该a1
得到getB
,因为它实际上是一个指针B
?