成员函数的重载、覆盖与隐藏
成员函数的重载、覆盖(override)与隐藏很容易混淆,C++程序员必须要搞清楚
概念,否则错误将防不胜防。
8.2.1 重载与覆盖
成员函数被重载的特征:
(1)相同的范围(在同一个类中);
(2)函数名字相同;
(3)参数不同;
(4)virtual 关键字可有可无。
覆盖是指派生类函数覆盖基类函数,特征是:
(1)不同的范围(分别位于派生类与基类);
(2)函数名字相同;
(3)参数相同;
(4)基类函数必须有virtual 关键字。
示例8-2-1 中,函数Base::f(int)与Base::f(float)相互重载,而Base::g(void)
被Derived::g(void)覆盖。
#include <iostream.h>
class Base
{
public:
void f(int x){ cout << "Base::f(int) " << x << endl; }
void f(float x){ cout << "Base::f(float) " << x << endl; }
virtual void g(void){ cout << "Base::g(void)" << endl;}
};
class Derived : public Base
{
public:
virtual void g(void){ cout << "Derived::g(void)" << endl;}
};
void main(void)
{
Derived d;
Base *pb = &d;
pb->f(42); // Base::f(int) 42
pb->f(3.14f); // Base::f(float) 3.14
pb->g(); // Derived::g(void)
}
示例8-2-1 成员函数的重载和覆盖
8.2.2 令人迷惑的隐藏规则
本来仅仅区别重载与覆盖并不算困难,但是C++的隐藏规则使问题复杂性陡然增加。
这里“隐藏”是指派生类的函数屏蔽了与其同名的基类函数,规则如下:
(1)如果派生类的函数与基类的函数同名,但是参数不同。此时,不论有无virtual
关键字,基类的函数将被隐藏(注意别与重载混淆)。
(2)如果派生类的函数与基类的函数同名,并且参数也相同,但是基类函数没有virtual
关键字。此时,基类的函数被隐藏(注意别与覆盖混淆)。
示例程序8-2-2(a)中:
(1)函数Derived::f(float)覆盖了Base::f(float)。
(2)函数Derived::g(int)隐藏了Base::g(float),而不是重载。
(3)函数Derived::h(float)隐藏了Base::h(float),而不是覆盖。
#include <iostream.h>
class Base
{
public:
virtual void f(float x){ cout << "Base::f(float) " << x << endl; }
void g(float x){ cout << "Base::g(float) " << x << endl; }
void h(float x){ cout << "Base::h(float) " << x << endl; }
};
class Derived : public Base
{
public:
virtual void f(float x){ cout << "Derived::f(float) " << x << endl; }
void g(int x){ cout << "Derived::g(int) " << x << endl; }
void h(float x){ cout << "Derived::h(float) " << x << endl; }
};
示例8-2-2(a)成员函数的重载、覆盖和隐藏
据作者考察,很多C++程序员没有意识到有“隐藏”这回事。由于认识不够深刻,
“隐藏”的发生可谓神出鬼没,常常产生令人迷惑的结果。
示例8-2-2(b)中,bp 和dp 指向同一地址,按理说运行结果应该是相同的,可事
实并非这样。
void main(void)
{
Derived d;
Base *pb = &d;
Derived *pd = &d;
// Good : behavior depends solely on type of the object
pb->f(3.14f); // Derived::f(float) 3.14
pd->f(3.14f); // Derived::f(float) 3.14
// Bad : behavior depends on type of the pointer
pb->g(3.14f); // Base::g(float) 3.14
pd->g(3.14f); // Derived::g(int) 3 (surprise!)
// Bad : behavior depends on type of the pointer
pb->h(3.14f); // Base::h(float) 3.14 (surprise!)
pd->h(3.14f); // Derived::h(float) 3.14
}
示例8-2-2(b) 重载、覆盖和隐藏的比较
8.2.3 摆脱隐藏
隐藏规则引起了不少麻烦。示例8-2-3 程序中,语句pd->f(10)的本意是想调用函
数Base::f(int),但是Base::f(int)不幸被Derived::f(char *)隐藏了。由于数字10
不能被隐式地转化为字符串,所以在编译时出错。
class Base
{
public:
void f(int x);
};
class Derived : public Base
{
public:
void f(char *str);
};
void Test(void)
{
Derived *pd = new Derived;
pd->f(10); // error
}
示例8-2-3 由于隐藏而导致错误
从示例8-2-3 看来,隐藏规则似乎很愚蠢。
众所周知,C++实现函数重载有两种情况:函数参数的类型不同,参数的个数不同。而与函数的返回值没有什么关系。
但:
- class TextBlock {
- public:
- ...
- const char& operator[] (std::size_t position) const
- {return text[position];}
- char& operator[] (std::size_t position)
- {return text[position];}
- private:
- std::string text;
- }
- TextBlock tb("Hello");
- std::cout << tb[0];
- const TextBlock ctb("World");
- std::cout << ctb[0];
const char& operator[] (std::size_t position) const
后一个const参与对重载函数的区分,这样在参数类型个数相同的情况下形成一种新的重载形式。
需要注意:常成员函数是常对象唯一的对外接口,使用使应该注意。常成员函数不能更新对象的数据成员,也不能调用该类中的普通成员函数。
这就是为什么const TextBlock ctb("World");会调用const char& operator[] (std::size_t position) const;的原因,包含了C++的隐式调用。
class testconst
{
public:
char& operator[](int postion) const
{
return pText[postion];
}
char& operator[](int postion)
{
return pText[postion];
}
void p()
{
cout<<text<<endl;
}
void p() const
{
cout<<num<<endl;
}
testconst(string str,char* pc,int a):text(str),pText(pc),num(a){}
void pp()
{
cout<<text<<endl;
}
private:
string text;
char* pText;
int num;
};
则:
string str = "string";
char* pc = "char";
const testconst test(str,pc,2);
//test.pp();//const 对象不能调用非CONST成员
test.p();//调用的是CONST的那个函数:void p() const
testconst test1(str,pc,2);
test1.pp();
test1.p();//调用的是非CONST的那个函数:void p()
如下表所示:
No. | 作用 | 说明 | 参考代码 |
1 | 可以定义const常量 |
| const int Max = 100; |
2 | 便于进行类型检查 | const常量有数据类型,而宏常量没有数据类型。编译器可以对前者进行类型安全检查,而对后者只进行字符替换,没有类型安全检查,并且在字符替换时可能会产生意料不到的错误 | void f(const int i) { .........} |
3 | 可以保护被修饰的东西 | 防止意外的修改,增强程序的健壮性。 | void f(const int i) { i=10;//error! } |
4 | 可以很方便地进行参数的调整和修改 | 同宏定义一样,可以做到不变则已,一变都变 |
|
5 | 为函数重载提供了一个参考 |
| class A |
6 | 可以节省空间,避免不必要的内存分配 | const定义常量从汇编的角度来看,只是给出了对应的内存地址,而不是象#define一样给出的是立即数,所以,const定义的常量在程序运行过程中只有一份拷贝,而#define定义的常量在内存中有若干个拷贝 | #define PI 3.14159 //常量宏 |
7 | 提高了效率 | 编译器通常不为普通const常量分配存储空间,而是将它们保存在符号表中,这使得它成为一个编译期间的常量,没有了存储与读内存的操作,使得它的效率也很高 |