Effective C++ item 36: Never Redefine an Inherited Non-virtual Function Value

Defining an inherited non-virtual function will produce unpredictable behavior.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
class B {
public:
void mf();
...
};
class D: public B {
public:
void mf();
...
};

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

As you can see, depend on which pointer the mf is called from, it calls different mf function. This is really bad because the behavior of such function call is unpredictable.

Reference:
“Effective C++” Third Edition by Scott Meyers.