我有三个不同的基类:
class BaseA
{
public:
virtual int foo() = 0;
};
class BaseB
{
public:
virtual int foo() { return 42; }
};
class BaseC
{
public:
int foo() { return 42; }
};
然后我从这样的基础派生(用 X 代替 A、B 或 C):
class Child : public BaseX
{
public:
int foo() { return 42; }
};
三个不同的基类中的函数是如何被覆盖的?我的以下三个假设是否正确?还有其他注意事项吗?
- 使用 BaseA,子类不会编译,纯虚函数没有定义。
- 使用 BaseB,当在 BaseB* 或 Child* 上调用 foo 时,将调用 child 中的函数。
- 使用 BaseC,当在 Child* 上调用 foo 而不是在 BaseB* 上调用子函数时(调用父类中的函数)。