您的位置:首页 > 编程语言 > C语言/C++

【C++基础】重载overload、重写(覆盖)override、隐藏hide的区别

2014-01-12 14:37 811 查看

重载overload

重载是指不同的函数使用相同的函数名,但是函数的参数个数或类型不同。调用的时候根据函数的参数来区别不同的函数。

相同的范围(在同一个类中)
函数名字相同
参数不同
virtual可有可无
编译期绑定,与多态无关

重写(覆盖)override

重写/覆盖是在在派生类中重新对基类中的虚函数重新实现;即函数名和参数都一样,只是函数的实现体不一样。

不同的范围(分别位于派生类和基类)
函数名相同
参数相同
基类函数必须有virtual关键字
运行时绑定,多态

隐藏hide

关于隐藏,参见: Effective C++:条款33:避免遮掩继承而来的名称

隐藏是指派生类中的函数把基类中相同名字的函数屏蔽掉;

如果派生类的函数与基类的函数同名,但参数不同,此时,不论有无virtual关键字,基类的函数将被隐藏
如果派生类的函数与基类的函数同名,但参数相同,此时,如果基类函数没有virtual关键字,基类的函数将被隐藏

#include <cstring>
#include <iostream>

using namespace std;

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; }
};

int main()
{
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

return 0;
}




函数Derived::f(float) 覆盖/重写了Base::f(float)
函数Derived::g(int) 隐藏了 Base::g(float)
函数Derived::h(float) 隐藏了 Base:h(float)

在第一种调用中,函数的行为取决于指针所指向的对象;在第二第三中调用中,函数的行为取决于指针的类型。所以说,隐藏破坏了多态

隐藏实例:

#include <cstring>
#include <iostream>

using namespace std;

class Base
{
public:
void f(float x){ }
};

class Derived : public Base
{
public:
void f(float x, float y){ }
void test()  { f(1.0f); }
};

int main()
{
return 0;
}


VS: error C2660: 'Derived::f' : function does not take 1 arguments

在派生类的域中,基类中的函数被隐藏。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐