C++ 中,我们该用哪种态度对待通过继承得到的名字.事情的实质与继承没什么关系。它与作用域有关。我们都知道它在代码中是这样的,
int x; // global variable
void someFunc()
{
double x; // local variable
{
double x; // local variable
std::cin >>
x
; // read a new value for local x
}
}
读入
x
的语句指涉 local 变量
x
,而不是 global 变量
x
,因为内层作用域的名字覆盖(“遮蔽”)外层作用域的名字。
当编译器在
someFunc
的作用域中遇到名字
x
时,他们巡视 local 作用域看看是否有什么东西叫这个名字。因为那里有,它们就不再检查其它作用域。在此例中,
someFunc
的
x
类型为
double
,而 global
x
类型为
int
,但这不要紧。C++ 的 name-hiding 规则仅仅是覆盖那个名字。而相对应的名字的类型是否相同是无关紧要的。在此例中,一个名为
x
的
double
覆盖了一个名为
x
的
int
。
加入 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;
private:
int x;
public:
virtual void mf1() = 0;
virtual void mf2();
void mf3();
virtual void mf1() = 0;
virtual void mf2();
void mf3();
...
};
};
class Derived: public Base {
public:
virtual void mf1();
void mf4();
public:
virtual void mf1();
void mf4();
...
};
};
本例中包含的既有 public 名字也有 private 名字,既有 data members 也有 member functions。member functions 既有 pure virtual 的,也有 simple (impure) virtual 的,还有 non-virtual 的。那是为了强调我们谈论的事情是关于名字的。例子中还可以包括其它类型的名字,例如,enums,nested classes,和 typedefs。在这里的讨论中唯一重要的事情是“它们是名字”。与它们是什么东西的名字毫不相关。这个示例中使用了 single inheritance,但是一旦你理解了在 single inheritance 下会发生什么,C++ 在 multiple inheritance 下的行为就很容易预见了。
假设
mf4
在 derived class 中被实现,其中一部分,如下:
void Derived::mf4()
{
{
...
mf2() ;
mf2() ;
...
}
}
当编译器看到这里对名字
mf2
的使用,它就必须断定它指涉什么。它通过搜索名为
mf2
的某物的定义的作用域来做这件事。首先它在 local 作用域中搜索(也就是
mf4
的作用域),但是它没有找到被称为
mf2
的任何东西的声明。然后它搜索它的包含作用域,也就是 class
Derived
的作用域。它依然没有找到叫做
mf2
的任何东西,所以它上移到它的上一层包含作用域,也就是 base class 的作用域。在那里它找到了名为
mf2
的东西,所以搜索停止。如果在
Base
中没有
mf2
,搜索还会继续,首先是包含
Base
的 namespace(s)(如果有的话),最后是 global 作用域。
上面描述的过程虽然是正确的,但它还不是一个关于 C++ 中名字如何被找到的完整的描述。无论如何,我们的目的不是为了充分了解关于写一个编译器时的名字搜索问题。而是为了充分了解如何避免令人吃惊的意外,而对于这个任务,我们已经有了大量的信息。
再次考虑前面的示例,而且这一次我们 overload
mf1
和
mf3
,并且为
Derived
增加一个
mf3
的版本。(
Derived
对
mf3
——一个通过继承得到的 non-virtual function ——的重载,使得这个设计立即变得可疑,但是出于对 inheritance 之下名字可见性问题的关心,我们就装作没看见。)
class Base {
private:
int x;
private:
int x;
public:
virtual void mf1() = 0;
virtual void mf1(int) ;
virtual void mf1() = 0;
virtual void mf1(int) ;
virtual void mf2();
void mf3();
void mf3(double) ;
...
};
void mf3(double) ;
...
};
class Derived: public Base {
public:
virtual void mf1();
void mf3() ;
void mf4();
...
};
public:
virtual void mf1();
void mf3() ;
void mf4();
...
};
以上代码导致的行为会使每一个第一次遇到它的 C++ 程序员吃惊。基于作用域的名字覆盖规则(scope-based name hiding rule)不会有什么变化,所以 base class 中的所有名为
mf1
和
mf3
的函数被 derived class 中的名为
mf1
和
mf3
的函数覆盖。从名字搜索的观点看,
Base::mf1
和
Base::mf3
不再被
Derived
继承!
Derived d;
int x;
int x;
...
d.mf1(); // fine, calls Derived::mf1
d.mf1(x); // error! Derived::mf1 hides Base::mf1
d.mf2(); // fine, calls Base::mf2
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
d.mf3(x); // error! Derived::mf3 hides Base::mf3
即使 base 和 derived classes 中的函数具有不同的参数类型,它也同样适用,而且不管函数是 virtual 还是 non-virtual,它也同样适用。与“在本 Item 的开始处,函数
someFunc
中的
double
x
覆盖了 global 作用域中的
int
x
”的道理相同,这里
Derived
中的函数
mf3
覆盖了具有不同类型的名为
mf3
的一个
Base
函数。
这一行为背后的根本原因是为了防止“当你在一个 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;
private:
int x;
public:
virtual void mf1() = 0;
virtual void mf1(int);
virtual void mf1() = 0;
virtual void mf1(int);
virtual void mf2();
void mf3();
void mf3(double);
...
};
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
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();
...
};
void mf3();
void mf4();
...
};
现在 inheritance 就可以起到预期的作用:
Derived d;
int x;
int x;
...
d.mf1(); // still fine, still calls Derived::mf1
d.mf1(x); // now okay, calls Base::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
d.mf3(x); // now okay, calls Base::mf3
这意味着如果你从一个带有重载函数的 base class 继承,而且你只想重定义或替换它们中的一部分,你需要为每一个你不想覆盖的名字使用
using
declaration。如果你不这样做,一些你希望继承下来的名字会被覆盖。
可以想象在某些时候你不希望从你的 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);
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)
...
};
public:
virtual void mf1() // forwarding function; implicitly
{ Base::mf1(); } // inline (see Item 30)
...
};
...
Derived d;
int x;
int x;
d.mf1(); // fine, calls Derived::mf1
d.mf1(x); // error! Base::mf1() is hidden
d.mf1(x); // error! Base::mf1() is hidden
forwarding function(转调函数)的另一个功效是用于老式的编译器,它们(不正确地)不支持用
using
declarations 将“通过继承得到的名字”引入到 derived class 的作用域。
这就是关于 inheritance 和 name hiding 的全部故事,但是当 inheritance 与 templates 结合起来,“通过继承得到的名字被隐藏”的问题会以一种全然不同的形式呈现出来。
Things to Remember
derived classes 中的名字覆盖 base classes 中的名字,在 public inheritance 中,这从来不是想要的。
为了使隐藏的名字重新可见,使用
using
declarations 或者 forwarding functions(转调函数)。