问题遇到的现象和发生背景
用代码块功能插入代码,请勿粘贴截图
#include<iostream>
using namespace std;
class A {
public:
A(){}
A(int x,int y):a(x),b(y){}
virtual int getA() {
return a;
}
int getB() {
return b;
}
private:
int a;
int b;
};
class B :public A {
public:
B(A) {}
int getA() {
return A::getA();
}
private:
int a;
int b;
};
int main() {
A* pa;
A a(2, 2);
B b(a);
pa = &a;
cout << "A::getA=" << pa->getA() << endl;
pa = &b;
cout << "B::getA=" << pa->getA() << endl;
return 0;
}
运行结果及报错内容
我想要达到的结果
B正确输出结果