menu

hjk41的日志

Avatar

Effective C++ 37: Never redefine an inherited nonvirtual function

Let B be the base class, and D be publicly derived from B. If B has a nonvirtual function mf(), then D should not redefine it.
D is publicly derived from B, so very D "isa" B, which means D can be used everywhere B can. Consider the following code:

D x;
B * b=&x;
D * d=&x;
b->mf();
d->mf();


Here, two calls to x.mf() will act differently while they are supposed to do the same thing.
In summary, redefining an inherited nonvirtual function is just a bad idea.

评论已关闭