摘自http://blog.csdn.net/yanjun_1982/archive/2005/09/02/470405.aspx
class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: #000000; text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">
class="title_txt" style="padding-right: 0px; padding-left: 1.5em; font-size: 15px; padding-bottom: 0.5em; margin: 0px; padding-top: 0px; border-bottom: #dcdcdc 2px solid; font-family: 'Microsoft yahei', verdana, sans-serif;">class="Apple-style-span" style="word-spacing: 0px; font: medium Simsun; text-transform: none; color: #000000; text-indent: 0px; white-space: normal; letter-spacing: normal; border-collapse: separate; orphans: 2; widows: 2; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; -webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: auto; -webkit-text-stroke-width: 0px;">class="Apple-style-span" style="font-size: 14px; line-height: 21px; font-family: verdana, 'ms song', 宋体, Arial, 微软雅黑, Helvetica, sans-serif; text-align: left;"> 这三个概念都是与OO中的多态有关系的。如果单是区别重载与覆盖这两个概念是比较容易的,但是隐藏这一概念却使问题变得有点复杂了,下面说说它们的区别吧。 class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> 重载是指不同的函数使用相同的函数名,但是函数的参数个数或类型不同。调用的时候根据函数的参数来区别不同的函数。
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> 覆盖(也叫重写)是指在派生类中重新对基类中的虚函数(注意是虚函数)重新实现。即函数名和参数都一样,只是函数的实现体不一样。
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> 隐藏是指派生类中的函数把基类中相同名字的函数屏蔽掉了。隐藏与另外两个概念表面上看来很像,很难区分,其实他们的关键区别就是在多态的实现上。什么叫多态?简单地说就是一个接口,多种实现吧。
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> 还是引用一下别人的代码来说明问题吧(引用自林锐的《高质量C/C++编程指南》)。
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">仔细看下面的代码:
class="MsoNormalTable" style="margin: 0px; border-collapse: collapse; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; padding: 0px;" border="1" cellspacing="0" cellpadding="0"> class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">#include <iostream.h>
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> class Base
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">{
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">public:
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> virtualclass="Apple-converted-space"> void f(class="tags" href="/tags/FLOAT.html" title=float>float x){ cout << "Base::f(class="tags" href="/tags/FLOAT.html" title=float>float) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 42.5pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">void g(class="tags" href="/tags/FLOAT.html" title=float>float x){ cout << "Base::g(class="tags" href="/tags/FLOAT.html" title=float>float) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> void h(class="tags" href="/tags/FLOAT.html" title=float>float x){ cout << "Base::h(class="tags" href="/tags/FLOAT.html" title=float>float) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">};
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> class Derived : public Base
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">{
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">public:
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> virtualclass="Apple-converted-space"> void f(class="tags" href="/tags/FLOAT.html" title=float>float x){ cout << "Derived::f(class="tags" href="/tags/FLOAT.html" title=float>float) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 42.5pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">void g(int x){ cout << "Derived::g(int) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> void h(class="tags" href="/tags/FLOAT.html" title=float>float x){ cout << "Derived::h(class="tags" href="/tags/FLOAT.html" title=float>float) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">};
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">看出什么了吗?下面说明一下:
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">(1)函数Derived::f(class="tags" href="/tags/FLOAT.html" title=float>float)覆盖了Base::f(class="tags" href="/tags/FLOAT.html" title=float>float)。
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">(2)函数Derived::g(int)隐藏了Base::g(class="tags" href="/tags/FLOAT.html" title=float>float),而不是重载。
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">(3)函数Derived::h(class="tags" href="/tags/FLOAT.html" title=float>float)隐藏了Base::h(class="tags" href="/tags/FLOAT.html" title=float>float),而不是覆盖。
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 21.8pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> 嗯,概念大概明白了,但是在实际的编程中,我们会因此遇到什么问题呢?再看下面的代码:
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 21.8pt; text-indent: 0px; line-height: 19px; padding: 0px;">void main(void)
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 21.8pt; text-indent: 0px; line-height: 19px; padding: 0px;">{
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">Derived class="Apple-converted-space"> d;
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">Base *pb = &d;
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">Derived *pd = &d;
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">// Good : class="tags" href="/tags/BEHAVIOR.html" title=behavior>behavior depends solely on type of the object
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">pb->f(3.14f);class="Apple-converted-space"> // Derived::f(class="tags" href="/tags/FLOAT.html" title=float>float) 3.14
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">pd->f(3.14f);class="Apple-converted-space"> // Derived::f(class="tags" href="/tags/FLOAT.html" title=float>float) 3.14
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">// Bad : class="tags" href="/tags/BEHAVIOR.html" title=behavior>behavior depends on type of the pointer
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">pb->g(3.14f);class="Apple-converted-space"> // Base::g(class="tags" href="/tags/FLOAT.html" title=float>float) 3.14
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">pd->g(3.14f);class="Apple-converted-space"> // Derived::g(int) 3 class="Apple-converted-space"> (surprise!)
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">
// Bad : class="tags" href="/tags/BEHAVIOR.html" title=behavior>behavior depends on type of the pointer
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">pb->h(3.14f);class="Apple-converted-space"> // Base::h(class="tags" href="/tags/FLOAT.html" title=float>float) 3.14 class="Apple-converted-space"> (surprise!)
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 30.95pt; text-indent: 0px; line-height: 19px; padding: 0px;">pd->h(3.14f);class="Apple-converted-space"> // Derived::h(class="tags" href="/tags/FLOAT.html" title=float>float) 3.14
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">}
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 21pt; text-indent: 21pt; line-height: 19px; padding: 0px;">在第一种调用中,函数的行为取决于指针所指向的对象。在第二第三种调用中,函数的行为取决于指针的类型。所以说,隐藏破坏了面向对象编程中多态这一特性,会使得OOP人员产生混乱。
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 21pt; text-indent: 21pt; line-height: 19px; padding: 0px;">不过隐藏也并不是一无是处,它可以帮助编程人员在编译时期找出一些错误的调用。但我觉得还是应该尽量使用隐藏这一些特性,该加virtual时就加吧。
class="post" style="margin: 0px; padding: 0px;">
重载(overload),覆盖(override),隐藏(hide)的区别
http://www.cppblog.com/zgysx/archive/2007/03/12/19662.html
写正题之前,先给出几个关键字的中英文对照,重载(overload),覆盖(override),隐藏(hide)。在早期的C++书籍中,可能翻译的人不熟悉专业用语(也不能怪他们,他们不是搞计算机编程的,他们是英语专业的),常常把重载(overload)和覆盖(override)搞错!
我们先来看一些代码及其编译结果。
实例一:
#include "stdafx.h"
#include <iostream.h>
class CB
{
public:
void f(int)
{
cout << "CB::f(int)" << endl;
}
};
class CD : public CB
{
public:
void f(int,int)
{
cout << "CD::f(int,int)" << endl;
}
void test()
{
f(1);
}
};
int main(int argc, char* argv[])
{
return 0;
}
编译了一下
error C2660: 'f' : function does not take 1 class="tags" href="/tags/PARAMETERS.html" title=parameters>parameters
结论:在类CD这个域中,没有f(int)这样的函数,基类中的void f(int)被隐藏
如果把派生CD中成员函数void f(int,int)的声明改成和基类中一样,即f(int),基类中的void f(int)还是一样被覆盖,此时编译不会出错,在函数中test调用的是CD中的f(int)
所以,在基类中的某些函数,如果没有virtral关键字,函数名是f(参数是什么我们不管),那么如果在派生类CD中也声明了某个f成员函数,那么在类CD域中,基类中所有的那些f都被隐藏。
如果你比较心急,想知道什么是隐藏,看文章最后的简单说明,不过我建议你还是一步一步看下去。
我们刚才说的是没有virtual的情况,如果有virtual的情况呢??
实例二:
#include "stdafx.h"
#include <iostream.h>
class CB
{
public:
virtual void f(int)
{
cout << "CB::f(int)" << endl;
}
};
class CD : public CB
{
public:
void f(int)
{
cout << "CD::f(int)" << endl;
}
};
int main(int argc, char* argv[])
{
return 0;
}
这么写当然是没问题了,在这里我不多费口舌了,这是很简单的,多态,虚函数,然后什么指向基类的指针指向派生类对象阿,通过引用调用虚函数阿什么的,属性多的很咯,什么??你不明白??随便找本C++的书,对会讲多态和虚函数机制的哦!!
这种情况我们叫覆盖(override)!覆盖指的是派生类的虚拟函数覆盖了基类的同名且参数相同的函数!
在这里,我要强调的是,这种覆盖,要满足两个条件
(a)有virtual关键字,在基类中函数声明的时候加上就可以了
(b)基类CB中的函数和派生类CD中的函数要一模一样,什么叫一模一样,函数名,参数,返回类型三个条件。
有人可能会对(b)中的说法质疑,说返回类型也要一样??
是,覆盖的话必须一样,我试了试,如果在基类中,把f的声明改成virtual int f(int),编译出错了
error C2555: 'CD::f' : overriding virtual function differs from 'CB::f' only by return type or calling convention
所以,覆盖的话,必须要满足上述的(a)(b)条件
那么如果基类CB中的函数f有关键字virtual ,但是参数和派生类CD中的函数f参数不一样呢,
实例三:
#include "stdafx.h"
#include <iostream.h>
class CB
{
public:
virtual void f(int)
{
cout << "CB::f(int)" << endl;
}
};
class CD : public CB
{
public:
void f(int,int)
{
cout << "CD::f(int,int)" << endl;
}
void test()
{
f(1);
}
};
int main(int argc, char* argv[])
{
return 0;
}
编译出错了,
error C2660: 'f' : function does not take 1 class="tags" href="/tags/PARAMETERS.html" title=parameters>parameters
咦??好面熟的错??对,和实例一中的情况一样哦,结论也是基类中的函数被隐藏了。
通过上面三个例子,得出一个简单的结论
如果基类中的函数和派生类中的两个名字一样的函数f
满足下面的两个条件
(a)在基类中函数声明的时候有virtual关键字
(b)基类CB中的函数和派生类CD中的函数一模一样,函数名,参数,返回类型都一样。
那么这就是叫做覆盖(override),这也就是虚函数,多态的性质
那么其他的情况呢??只要名字一样,不满足上面覆盖的条件,就是隐藏了。
下面我要讲最关键的地方了,好多人认为,基类CB中的f(int)会继承下来和CD中的f(int,int)在派生类CD中构成重载,就像实例一中想像的那样。
对吗?我们先看重载的定义
重载(overload):
必须在一个域中,函数名称相同但是函数参数不同,重载的作用就是同一个函数有不同的行为,因此不是在一个域中的函数是无法构成重载的,这个是重载的重要特征
必须在一个域中,而继承明显是在两个类中了哦,所以上面的想法是不成立的,我们测试的结构也是这样,派生类中的f(int,int)把基类中的f(int)隐藏了
所以,相同的函数名的函数,在基类和派生类中的关系只能是覆盖或者隐藏。
在文章中,我把重载和覆盖的定义都给了出来了,但是一直没有给隐藏的定义,在最后,我把他给出来,这段话是网上google来的,比较长,你可以简单的理解成,在派生类域中,看不到基类中的那个同名函数了,或者说,是并没有继承下来给你用,呵呵,如实例一 那样。
隐藏(hide):
指的是派生类的成员函数隐藏了基类函数的成员函数.隐藏一词可以这么理解:在调用一个类的成员函数的时候,class="tags" href="/tags/BianYiQi.html" title=编译器>编译器会沿着类的继承链逐级的向上查找函数的定义,如果找到了那么就停止查找了,所以如果一个派生类和一个基类都有同一个同名(暂且不论参数是否相同)的函数,而class="tags" href="/tags/BianYiQi.html" title=编译器>编译器最终选择了在派生类中的函数,那么我们就说这个派生类的成员函数"隐藏"了基类的成员函数,也就是说它阻止了class="tags" href="/tags/BianYiQi.html" title=编译器>编译器继续向上查找函数的定义.
class="Apple-interchange-newline" /> class="MsoNormal" style="margin: 0cm 0cm 0pt;">
class="MsoNormal" style="margin: 0cm 0cm 0pt;">class="apple-style-span">class="apple-style-span">
class="apple-style-span"> class="MsoNormal" style="margin: 0cm 0cm 0pt;">class="apple-style-span">摘自:class="apple-style-span">http://blog.csdn.net/yakihappy/archive/2009/03/11/3979333.aspx
class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 18pt; line-height: 14.25pt; text-align: left; mso-pagination: widow-orphan;" align="left">多态性是指在一个程序中同名的不同方法共存的情况,允许不同类的对象对同一消息作出不同响应。
class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 18pt; line-height: 14.25pt; text-align: left; mso-pagination: widow-orphan;" align="left">静态多态性即是编译时的多态 这种多态采用函数重载(模板)来实现,class="tags" href="/tags/BianYiQi.html" title=编译器>编译器对对象静态绑定,编译之后就决定了调用哪一个成员函数。覆盖:在子类中直接定义的和父类同名的属性和方法,在子类的使用中, 子类覆盖父类 ;重载:在同一个类中可以有多个同名的方法(不同的形参,不同的实现)。 调用时根据形参的个数和类型来决定调用那个方法,如多个构造函数的重载。在使用方法重载时无法在同一个类中声明签名(方法名、参数数目、参数类型)相同但返回类型不同的方法。 将方法重载称为静态方法绑定或先期绑定,这是由于在编译时class="tags" href="/tags/BianYiQi.html" title=编译器>编译器会根据参数的类型和数目来决定调用哪个方法,而不是在执行时决定。
class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 18pt; line-height: 14.25pt; text-align: left; mso-pagination: widow-orphan;" align="left">动态多态性是由继承和虚函数实现的,class="tags" href="/tags/BianYiQi.html" title=编译器>编译器对对象动态绑定,具体调用哪一个函数只有到执行的时候才知道。C++中,多态通过虚函数实现,Java使用abstract类实现多态。
class="MsoNormal" style="margin: 0cm 0cm 0pt; text-indent: 18pt; line-height: 14.25pt; text-align: left; mso-pagination: widow-orphan;" align="left">动态多态如:加工厂模式的代码结构,里面有基类,在基类里面定义了一个虚函数,然后这个虚函数的多个具体的实现是在基类的各个派生类里面实现的。在调用的地方还是同一个函数调用,然后根据具体的场合调用不同的对象中的相应的函数,这种代码形式实现了运行时的动态
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">#include <iostream.h>
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> class Base
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">{
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">public:
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> virtualclass="Apple-converted-space"> void f(class="tags" href="/tags/FLOAT.html" title=float>float x){ cout << "Base::f(class="tags" href="/tags/FLOAT.html" title=float>float) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 42.5pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">void g(class="tags" href="/tags/FLOAT.html" title=float>float x){ cout << "Base::g(class="tags" href="/tags/FLOAT.html" title=float>float) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> void h(class="tags" href="/tags/FLOAT.html" title=float>float x){ cout << "Base::h(class="tags" href="/tags/FLOAT.html" title=float>float) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">};
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> class Derived : public Base
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">{
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">public:
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> virtualclass="Apple-converted-space"> void f(class="tags" href="/tags/FLOAT.html" title=float>float x){ cout << "Derived::f(class="tags" href="/tags/FLOAT.html" title=float>float) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt 42.5pt; text-indent: 21.25pt; line-height: 19px; padding: 0px;">void g(int x){ cout << "Derived::g(int) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 0px; line-height: 19px; padding: 0px;"> class="Apple-converted-space"> void h(class="tags" href="/tags/FLOAT.html" title=float>float x){ cout << "Derived::h(class="tags" href="/tags/FLOAT.html" title=float>float) " << x << endl; }
class="MsoNormal" style="font-size: 13px; margin: 0cm 0cm 0pt; text-indent: 21pt; line-height: 19px; padding: 0px;">};
// Bad : class="tags" href="/tags/BEHAVIOR.html" title=behavior>behavior depends on type of the pointer
重载(overload),覆盖(override),隐藏(hide)的区别
http://www.cppblog.com/zgysx/archive/2007/03/12/19662.html
写正题之前,先给出几个关键字的中英文对照,重载(overload),覆盖(override),隐藏(hide)。在早期的C++书籍中,可能翻译的人不熟悉专业用语(也不能怪他们,他们不是搞计算机编程的,他们是英语专业的),常常把重载(overload)和覆盖(override)搞错!
我们先来看一些代码及其编译结果。
实例一:
#include "stdafx.h"
#include <iostream.h>
class CB
{
public:
void f(int)
{
cout << "CB::f(int)" << endl;
}
};
class CD : public CB
{
public:
void f(int,int)
{
cout << "CD::f(int,int)" << endl;
}
void test()
{
f(1);
}
};
int main(int argc, char* argv[])
{
return 0;
}
编译了一下
error C2660: 'f' : function does not take 1 class="tags" href="/tags/PARAMETERS.html" title=parameters>parameters
结论:在类CD这个域中,没有f(int)这样的函数,基类中的void f(int)被隐藏
如果把派生CD中成员函数void f(int,int)的声明改成和基类中一样,即f(int),基类中的void f(int)还是一样被覆盖,此时编译不会出错,在函数中test调用的是CD中的f(int)
所以,在基类中的某些函数,如果没有virtral关键字,函数名是f(参数是什么我们不管),那么如果在派生类CD中也声明了某个f成员函数,那么在类CD域中,基类中所有的那些f都被隐藏。
如果你比较心急,想知道什么是隐藏,看文章最后的简单说明,不过我建议你还是一步一步看下去。
我们刚才说的是没有virtual的情况,如果有virtual的情况呢??
实例二:
#include "stdafx.h"
#include <iostream.h>
class CB
{
public:
virtual void f(int)
{
cout << "CB::f(int)" << endl;
}
};
class CD : public CB
{
public:
void f(int)
{
cout << "CD::f(int)" << endl;
}
};
int main(int argc, char* argv[])
{
return 0;
}
这么写当然是没问题了,在这里我不多费口舌了,这是很简单的,多态,虚函数,然后什么指向基类的指针指向派生类对象阿,通过引用调用虚函数阿什么的,属性多的很咯,什么??你不明白??随便找本C++的书,对会讲多态和虚函数机制的哦!!
这种情况我们叫覆盖(override)!覆盖指的是派生类的虚拟函数覆盖了基类的同名且参数相同的函数!
在这里,我要强调的是,这种覆盖,要满足两个条件
(a)有virtual关键字,在基类中函数声明的时候加上就可以了
(b)基类CB中的函数和派生类CD中的函数要一模一样,什么叫一模一样,函数名,参数,返回类型三个条件。
有人可能会对(b)中的说法质疑,说返回类型也要一样??
是,覆盖的话必须一样,我试了试,如果在基类中,把f的声明改成virtual int f(int),编译出错了
error C2555: 'CD::f' : overriding virtual function differs from 'CB::f' only by return type or calling convention
所以,覆盖的话,必须要满足上述的(a)(b)条件
那么如果基类CB中的函数f有关键字virtual ,但是参数和派生类CD中的函数f参数不一样呢,
实例三:
#include "stdafx.h"
#include <iostream.h>
class CB
{
public:
virtual void f(int)
{
cout << "CB::f(int)" << endl;
}
};
class CD : public CB
{
public:
void f(int,int)
{
cout << "CD::f(int,int)" << endl;
}
void test()
{
f(1);
}
};
int main(int argc, char* argv[])
{
return 0;
}
编译出错了,
error C2660: 'f' : function does not take 1 class="tags" href="/tags/PARAMETERS.html" title=parameters>parameters
咦??好面熟的错??对,和实例一中的情况一样哦,结论也是基类中的函数被隐藏了。
通过上面三个例子,得出一个简单的结论
如果基类中的函数和派生类中的两个名字一样的函数f
满足下面的两个条件
(a)在基类中函数声明的时候有virtual关键字
(b)基类CB中的函数和派生类CD中的函数一模一样,函数名,参数,返回类型都一样。
那么这就是叫做覆盖(override),这也就是虚函数,多态的性质
那么其他的情况呢??只要名字一样,不满足上面覆盖的条件,就是隐藏了。
下面我要讲最关键的地方了,好多人认为,基类CB中的f(int)会继承下来和CD中的f(int,int)在派生类CD中构成重载,就像实例一中想像的那样。
对吗?我们先看重载的定义
重载(overload):
必须在一个域中,函数名称相同但是函数参数不同,重载的作用就是同一个函数有不同的行为,因此不是在一个域中的函数是无法构成重载的,这个是重载的重要特征
必须在一个域中,而继承明显是在两个类中了哦,所以上面的想法是不成立的,我们测试的结构也是这样,派生类中的f(int,int)把基类中的f(int)隐藏了
所以,相同的函数名的函数,在基类和派生类中的关系只能是覆盖或者隐藏。
在文章中,我把重载和覆盖的定义都给了出来了,但是一直没有给隐藏的定义,在最后,我把他给出来,这段话是网上google来的,比较长,你可以简单的理解成,在派生类域中,看不到基类中的那个同名函数了,或者说,是并没有继承下来给你用,呵呵,如实例一 那样。
隐藏(hide):
指的是派生类的成员函数隐藏了基类函数的成员函数.隐藏一词可以这么理解:在调用一个类的成员函数的时候,class="tags" href="/tags/BianYiQi.html" title=编译器>编译器会沿着类的继承链逐级的向上查找函数的定义,如果找到了那么就停止查找了,所以如果一个派生类和一个基类都有同一个同名(暂且不论参数是否相同)的函数,而class="tags" href="/tags/BianYiQi.html" title=编译器>编译器最终选择了在派生类中的函数,那么我们就说这个派生类的成员函数"隐藏"了基类的成员函数,也就是说它阻止了class="tags" href="/tags/BianYiQi.html" title=编译器>编译器继续向上查找函数的定义.
class="title_txt" style="padding-right: 0px; padding-left: 1.5em; font-size: 15px; padding-bottom: 0.5em; margin: 0px; padding-top: 0px; border-bottom: #dcdcdc 2px solid; font-family: 'Microsoft yahei', verdana, sans-serif;">而关于多态还有一比较简单的描述:
class="MsoNormal" style="margin: 0cm 0cm 0pt;">class="apple-style-span">基类有虚函数的时候,类中有虚函数指针,指向class="apple-style-span">vtable,class="apple-style-span">该class="apple-style-span">vtableclass="apple-style-span">中是基类的函数实现版本。如果子类有自己的实现,会在自己的虚函数表中替换子类的实现版本。当通过指针访问时候,通过class="apple-style-span">vptrclass="apple-style-span">定位到的函数就是具体的类的实现版本。就是多态。