条款36:绝不重新定义继承而来的 non-virtual 函数

Never redefine an inherited non-virtual function.

class B {
public:
    void mf();
};

class D:public B {};

D x;
B* pB = &x;
pB->mf();    // 调用B::mf
D* pD = &x;
pD->mf();    // 调用D::mf

造成这的原因在于,non-virtual 函数都是静态绑定(statically bound),而 virtual 函数却是动态绑定(dynamically bound)。

Last updated

Was this helpful?