zoukankan      html  css  js  c++  java
  • 关于虚函数,纯虚函数,虚基类

    在此参考了,ElliottZC和心晴工作室的博客:

         http://www.cppblog.com/ElliottZC/archive/2007/07/20/28417.html

         http://www.cnblogs.com/ms-frank/archive/2008/01/16/1041310.html

    需要注意的地方:

    1、定义一个函数为虚函数,不代表函数为不被实现的函数。定义他为虚函数是为了允许用基类的指针来调用子类的这个函数。

    2、定义一个函数为纯虚函数,才代表函数没有被实现。定义他是为了实现一个接口,起到一个规范的作用,规范继承这个。类的程序员必须实现这个函数。

    3、有纯虚函数的类是不可能生成类对象的,如果没有纯虚函数则可以。

    4、虚函数在多态中间的使用:   多态一般就是通过指向基类的指针来实现的。

    5、虚基类,保证了只保留一个实例。

    6、当指向不明是,没有引入虚函数时,不管引用的实例是哪个类的当你调用的时候系统会调用左值那个对象所属类的方法。

        A *p; //定义基类的指针
        A a;
        B b;
        p=&a;
        p->funPrint();
        p=&b;
        p->funPrint();
    //因此输出都是A的funPrint()函数的内容。
  • 相关阅读:
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
    Data Structure and Algorithm
  • 原文地址:https://www.cnblogs.com/Akunwjy/p/3460266.html
Copyright © 2011-2022 走看看