·一周点击排行
·热点推荐
C++箴言:避免覆盖通过继承得到的名字
发布时间:2008-5-29 17:05:00 浏览次数: 411
莎士比亚有一个关于名字的说法。"What’s in a name?" 他问道,"A rose by any other name would smell as sweet."(语出《罗密欧与朱丽叶》第二幕第二场,朱生豪先生译为:“姓名本来是没有意义的;我们叫做玫瑰的这一种花,要是换了个名字,他的香味还是同样的芬芳。”梁实秋先生译为:“姓算什么?我们所谓有玫瑰,换个名字,还是一样的香。”——译者注)。莎翁也写过 "he that filches from me my good name ... makes me poor indeed."(语出《奥塞罗》第三幕第三场,朱生豪先生译为:“可是谁偷去了我的名誉,那么他虽然并不因此而富足,我却因为失去它而成为赤贫了。”梁实秋先生译为:“但是他若夺去我的名誉,于他不见有利,对我却是一件损失哩。”——译者注)。好吧,在 C++ 中,我们该用哪种态度对待通过继承得到的名字呢?
事情的实质与继承没什么关系。它与作用域有关。我们都知道它在代码中是这样的,
| int x; // global variable void someFunc() { double x; // local variable std::cin >> x; // read a new value for local x } |
|
加入 inheritance 以后。我们知道当我们在一个 derived class member function 内指涉位于 base class 内的一件东西(例如,一个 member function,一个 typedef,或者一个 data member)时,编译器能够找到我们指涉的东西是因为 derived classes 继承到声明于 base classes 中的东西。实际中的运作方法是将 derived class 的作用域嵌套在 base class 作用域之中。例如:
| class Base { private: int x; public: virtual void mf1() = 0; virtual void mf2(); void mf3(); ... }; class Derived: public Base { public: virtual void mf1(); void mf4(); ... }; |
|
假设 mf4 在 derived class 中被实现,其中一部分,如下:
| void Derived::mf4() { ... mf2(); ... } |
我刚刚描述的过程虽然是正确的,但它还不是一个关于 C++ 中名字如何被找到的完整的描述。无论如何,我们的目的不是为了充分了解关于写一个编译器时的名字搜索问题。而是为了充分了解如何避免令人吃惊的意外,而对于这个任务,我们已经有了大量的信息。
再次考虑前面的示例,而且这一次我们 overload mf1 和 mf3,并且为 Derived 增加一个 mf3 的版本。(Derived 对 mf3 ——一个通过继承得到的 non-virtual function ——的重载,使得这个设计立即变得可疑,但是出于对 inheritance 之下名字可见性问题的关心,我们就装作没看见。)
| class Base { private: int x; public: virtual void mf1() = 0; virtual void mf1(int); virtual void mf2(); void mf3(); void mf3(double); ... }; class Derived: public Base { public: virtual void mf1(); void mf3(); void mf4(); ... }; |
|
| Derived d; int x; ... d.mf1(); // fine, calls Derived::mf1 d.mf1(x); // error! Derived::mf1 hides Base::mf1 d.mf2(); // fine, calls Base::mf2 d.mf3(); // fine, calls Derived::mf3 d.mf3(x); // error! Derived::mf3 hides Base::mf3 |
这一行为背后的根本原因是为了防止“当你在一个 library 或者 application Framework 中创建一个新的 derived class 时,偶然地发生从遥远的 base classes 继承 overloads 的情况”。不幸的是,一般情况下你是需要继承这些 overloads 的。实际上,如果你使用了 public inheritance 而又没有继承这些 overloads,你就违反了“base 和 derived classes 之间是 is-a 关系”这一 public inheritance 的基本原则。在这种情况下,你几乎总是要绕过 C++ 对“通过继承得到的名字”的缺省的覆盖机制。
你可以用 using declarations 做到这一点:
| class Base { private: int x; public: virtual void mf1() = 0; virtual void mf1(int); virtual void mf2(); void mf3(); void mf3(double); ... }; class Derived: public Base { public: using Base::mf1; // make all things in Base named mf1 and mf3 using Base::mf3; // visible (and public) in Derived’s scope virtual void mf1(); void mf3(); void mf4(); ... }; |
|
| Derived d; int x; ... d.mf1(); // still fine, still calls Derived::mf1 d.mf1(x); // now okay, calls Base::mf1 d.mf2(); // still fine, still calls Base::mf2 d.mf3(); // fine, calls Derived::mf3 d.mf3(x); // now okay, calls Base::mf3 |
可以想象在某些时候你不希望从你的 base classes 继承所有的函数。在 public inheritance 中,这是绝不会发生的,这还是因为,它违反了 public inheritance 在 base 和 derived classes 之间的 is-a 关系。(这就是为什么上面的 using declarations 在 derived class 的 public 部分:在 base class 中是 public 的名字在公有继承的 derived class 中也应该是 public。)然而,在 private inheritance中,它还是有意义的。例如,假设 Derived 从 Base 私有继承,而且 Derived 只想继承没有参数的那个 mf1 的版本。在这里,using declaration 没有这个本事,因为一个 using declaration 会使得所有具有给定名字的函数在 derived class 中可见。不,这里是使用了一种不同的技术的情形,即,一个简单的 forwarding function(转调函数):
| class Base { public: virtual void mf1() = 0; virtual void mf1(int); ... // as before }; class Derived: private Base { public: virtual void mf1() // forwarding function; implicitly { Base::mf1(); } // inline (see Item 30) ... }; ... Derived d; int x; d.mf1(); // fine, calls Derived::mf1 d.mf1(x); // error! Base::mf1() is hidden |
这就是关于 inheritance 和 name hiding 的全部故事,但是当 inheritance 与 templates 结合起来,“通过继承得到的名字被隐藏”的问题会以一种全然不同的形式呈现出来。关于全部 angle-bracket-demarcated(边边角角)的细节。
Things to Remember
·derived classes 中的名字覆盖 base classes 中的名字,在 public inheritance 中,这从来不是想要的。
·为了使隐藏的名字重新可见,使用 using declarations 或者 forwarding functions(转调函数)。
讨论此主题请进>>: C++箴言:避免覆盖通过继承得到的名字



