【发布时间】:2021-01-25 18:09:11
【问题描述】:
#include <iostream>
using namespace std;
class A {
public:
virtual void f() = 0;
};
class B : public A {
public:
void f() {cout << "hi" << endl;}
void g() { cout << "bye" << endl; }
};
int main() {
B b;
A &a = b;
a.f(); // prints "hi"
a.g(); // compile error no member g()
return 0;
}
为什么a.g() 在a.f() 调用B 的f() 时出现编译错误?
在A &a = b;,编译器是否以某种方式设置了 a 的别名内存结束的边界?
【问题讨论】:
标签: c++ inheritance reference abstract-class