• c++多态之动态绑定


    C++的函数调用默认不使用动态绑定。要触发动态绑定,必须满足两个条件:

    1. 只有指定为虚函数的成员函数才能进行动态绑定
    2. 必须通过基类类型的引用或指针进行函数调用

    因为每个派生类对象中都拥有基类部分,所以可以使用基类类型的指针或引用来引用派生类对象

    示例

    #include <iostream>
    #include <string>
    using namespace std;
    
    struct base
    {
        base(string str = "Base") : basename(str) {}
        virtual void print() { cout << basename << endl; }
        private:
            string basename;
    };
    struct derived : public base
    {
        derived(string str = "Derived") : derivedname(str) {}
        void print() { cout << derivedname << endl; }
        private:
            string  derivedname;
    };
    
    int main()
    {
        base b;
        derived d;
        cout << "b.print(), d.print()" << endl;
        b.print();
        d.print();
    
        base *pb = &b;
        base *pd = &d;
        cout << "pb->print(), pd->print()" << endl;
        pb->print();
        pd->print();
    
        base &yb = b;
        base &yd = d;
        cout << "yb.print(), yd.print()" << endl;
        yb.print();
        yd.print();
    }
    

    结果

    img

    分析

    可以看出基类类型的指针或引用来引用派生类对象时,调用的是重定义的虚函数。要想覆盖虚函数机制,调用基函数的版本,可以使用强制措施。例:

    代码

    #include <iostream>
    #include <string>
    using namespace std;
    
    struct base
    {
        base(string str = "Base") : basename(str) {}
        virtual void print() { cout << basename << endl; }
        private:
            string basename;
    };
    struct derived : public base
    {
        derived(string str = "Derived") : derivedname(str) {}
        void print() { cout << derivedname << endl; }
        private:
            string  derivedname;
    };
    
    int main()
    {
        base b;
        derived d;
        cout << "b.print(), d.print()" << endl;
        b.print();
        d.base::print();
    
        base *pb = &b;
        base *pd = &d;
        cout << "pb->print(), pd->print()" << endl;
        pb->print();
        pd->base::print();
    
        base &yb = b;
        base &yd = d;
        cout << "yb.print(), yd.print()" << endl;
        yb.print();
        yd.base::print();
    }
    

    结果

    img

    转自https://www.cnblogs.com/kaituorensheng/p/3509593.html

  • 相关阅读:
    Spring经典视频教程大集合
    使用jsonlib进行Java和JSON之间的转换
    c:import Unable to get RequestDispatcher for Context
    打开IE都会弹出欢迎界面“欢迎使用Internet Explorer 8”
    Struts2 标签 用法示例 (
    as3相关的frameworks
    as相关的框架引擎应用集合
    init
    XNA框架基础——疑难解答
    XNA框架基础——XNA的注意事项
  • 原文地址:https://www.cnblogs.com/Chlik/p/13555761.html
Copyright © 2020-2023  润新知