80

深度讲解C++的继承、重载、隐藏

 5 years ago
source link: http://www.10tiao.com/html/461/201806/2650716509/2.html
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.

继承,重载的概念对于一位C++程序员来说就是轻车驾熟,而隐藏和覆盖也并不陌生。但是,当我们写了一个自认为是重载的函数却报错的时候,就又重燃怀疑之火了。那么问题来了,在派生类继承基类的时候,若有同名函数就会发生隐藏、覆盖和重载这几种情况,该如何区分和理解呢?

继承

基本理解

继承是面向对象编程的一个基本概念(另外,还有数据抽象和动态绑定),至于其中的许多规则(例如修饰符)就先不去深究了,我们只要理解最基础的一点:派生类继承基类的成员。而我们测试代码里面都用public继承的方式,以免去不必要的麻烦。

存储结构

看下面的存储结构,对于后面的理解很有帮助。

  • 基类和派生类都有各自的作用域,在书中也会经常看到派生类会把父类的成员复制一份过来的说法。

代码

可以看一个简单的例子:

#include 
  
            using namespace std; class Base{        int a;        int b; }; class Derived : public Base{        int b;        int c; }; class Base1{ private:        int a;        int b; }; class Derived1 : private Base1{        int b;        int c; }; int main() {   cout << "sizeof Derived: " 
       << sizeof(Derived) << endl; // 16   cout << "sizeof Base: "
       << sizeof(Base) << endl;  // 8   cout << "sizeof Derived1: "
       << sizeof(Derived1) << endl; // 16   cout << "sizeof Base1: "
       << sizeof(Base1) << endl; // 8        getchar();        return 0; }
  • 不管是public和private继承,派生类都是16个字节,说明都拷贝过来了。只是说private的继承不能访问基类的private成员。

隐藏

看到存储结构图,再联想到局部变量隐藏全局变量的规则,也可以理解派生类同名成员会隐藏基类成员。并且,使用显示调用方法还是可以调用基类成员函数的。例如:d.Base::b和d.Base::fBase()

注意:这里的隐藏会和重载有时候会混淆!小结中的两点可以用来帮助区分。

代码

隐藏的测试代码:

class Base{    public:       int a;       int b;       void fBase()
      {
          cout << "Base class function fBase()"
               << endl;
      } }; class Derived : public Base{ p    ublic:       int b;       int c;       void fDerived()
      {
          cout << "Derived class function fDerived()"
               << endl;
      }       void fBase()
      {
          cout << "Derived class function fBase()"
               << endl;
      } }; int main() {       Derived d;       cout << d.c << endl;       cout << d.b << endl;       cout << d.Base::b << endl;       d.fDerived();       d.fBase();       d.Base::fBase();       getchar();       return 0; }
  • 关于作用域,《C++ primer》有一段如下的描述:

在派生类作用域中派生类成员将屏蔽基类成员。即使函数原型不同,基类成员也会被屏蔽。

  • 另外,至于为什么函数原型不同,也会屏蔽的原因跟编译器有关。

名字查找在编译时发生。一旦在子类中找到了名字,就不再继续查找了。

重载

像其他任意函数一样,成员函数(无论虚还是非虚)也可以重载。派生类可以重定义所有继承的0个或多个版本。通过前面的覆盖我们可以得到以下一些注意事项:

  • 如果派生类重新定义了重载成员,则通过派生类型只能访问派生类中定义的那些成员。

  • 如果派生类想通过自身类型使用所有的重载版本,则派生类型必须要么重定义所有重载版本,要么一个也不定义。

代码:

class Base{    public:       int a;       int b;       void fBase()
      {
          cout << "Base class function fBase()"
               << endl;
      }       void fBase(int a)
      {
          cout << "Base class function fBase(" << a << ")."
               << endl;
      } }; class Derived : public Base{    public:       int b;       int c;       void fDerived()
      {
          cout << "Derived class function fDerived()"
               << endl;
      }       void fBase()
      {
          cout << "Derived class function fBase()"
               << endl;
      } }; int main() {       Derived d;       d.fBase();       //d.fBase(2); //error: no matching function for call to 'Derived::fBase(int)'       d.Base::fBase(3);       getchar();       return 0; }
error: no matching function for call to 'Derived::fBase(int)'
  • 一种解决办法是在派生类中申明基类的函数,就可以不用重新定义重载函数的所有版本了

代码:

class Derived : public Base{    public:       using Base::fBase;       int b;       int c;       void fDerived() 
      {
           cout << "Derived class function fDerived()"
                << endl;
      }
              void fBase()
      {
          cout << "Derived class function fBase()"
               << endl;
      } };

特别注意:加入的using申明需要是public修饰的,否则编译的时候会提示:”error: ‘void Base::fBase(int)’ is inaccessible“的错误。

覆盖

覆盖出现的两个条件:

  • 只发生在虚函数的情况下

  • 且基类和派生类的成员函数类型必须一模一样,即参数和返回类型都必须一致

所以,派生类对象调用时,会调用派生类的成员函数。但是,也可以通过显示调用父类的同名成员函数。把上面的例子fBase()函数改成虚函数,加上virtual关键字,结果还是一样的[1]。

代码


virtual void fBase()
{
   cout << "Base class function fBase()"
        << endl;
}

结果:

Derived class function fBase()
Base class function fBase(2).
Base class function fBase(3).

总结

  • 派生类是将基类的所有成员都复制一份,并且保存在不同的域中。

  • 重载是在同一作用域下面的,基类和派生类不在同一个域,属于隐藏情况。

  • 覆盖其实也是隐藏的一种,只是意义上是不同的。







About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK