设为首页 加入收藏

TOP

C++ 多重继承详细教程(三)
2014-02-14 12:51:47 来源: 作者: 【 】 浏览:356
Tags:  多重 继承 详细 教程


    ***多重继承歧义函数解决定式(multiple inheritance function ambiguity idiom)。
    例:***
    #include<iostream>
    using namespace std;
    class R
    {
    int r;
    public:
    R(int x=0):r(x) {}
    void f(){cout 《 "r=" 《 endl;}
    };
    class A:virtual public R
    {
    int a;
    protected:
    void fA(){cout 《 "a=" 《 a 《 endl;};
    public:
    A(int x, int y):R(x), a(y){}
    void f() {fA();R::f();}  //看不出区别;
    };
    class B:virtual public R
    {
    int b;
    protected:
    void fB(){cout 《 "b=" 《 b 《 endl;};
    public:
    B(int x, int y):R(x),b(y){}
    void f() {fB();R::f();}  //看不出区别;
    };
    class C:public A, public B
    {
    int c;
    protected:
    void fC() {cout 《 "c=" 《 c 《 endl;};
    public:
    C(int x, int y, int z, int w):R(x),A(x,y),B(x,z),c(w) {}
    void f()   //拆分后只调用了一次f();
    {
    R::f();
    A::fA();
    B::fB();
    fC();
    }
    };
    void main()
    {
    R rr(1000);
    A aa(2222,444);
    B bb(3333,111);
    C cc(1212,345,123,45);
    cc.f();
    }

      

首页 上一页 1 2 3 下一页 尾页 3/3/3
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇一个n*n的图是否能由十字架拼成 下一篇用链表解决if语句过多的问题

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·HTTPS 详解一:附带 (2025-12-26 02:20:37)
·TCP/IP协议到底在讲 (2025-12-26 02:20:34)
·TCP和UDP在socket编 (2025-12-26 02:20:32)
·有没有适合新手练习 (2025-12-26 01:48:47)
·用清华镜像网怎么下 (2025-12-26 01:48:44)