VC语言

本类阅读TOP10

·VC++ 学习笔记(二)
·用Visual C++打造IE浏览器(1)
·每个开发人员现在应该下载的十种必备工具
·教你用VC6做QQ对对碰外挂程序
·Netmsg 局域网聊天程序
·Windows消息大全
·VC++下使用ADO编写数据库程序
·VC++学习笔记(四)
·非法探取密码的原理及其防范
·怎样在VC++中访问、修改注册表

分类导航
VC语言Delphi
VB语言ASP
PerlJava
Script数据库
其他语言游戏开发
文件格式网站制作
软件工程.NET开发
Guru of the Week:#38 多重继承 Ⅱ.

作者:未知 来源:月光软件站 加入时间:2005-2-28 月光软件站

/*此文是译者出于自娱翻译的GotW(Guru of the Week:http://www.gotw.ca/gotw/index.htm)系列文章的一篇,原文的版权是属于Hub Sutter(著名的C++专家,《Exceptional C++》的作者)。此文的翻译没有征得原作者的同意,只供学习讨论。——译者:黄森堂*/

#38 多重继承 Ⅱ.

难度:8/10

如何你不能使用多重继承,如何模仿它吗?,不要忘记模仿可能出在客户端的自然语法。

问题:

1.思考以下代码:

  struct A      { virtual ~A() { }
                  virtual string Name() { return "A";  } };
  struct B1 : virtual A { string Name() { return "B1"; } };
  struct B2 : virtual A { string Name() { return "B2"; } };

  struct D  : B1, B2    { string Name() { return "D";  } };

在以上示例中,如果不使用多重继承,如何写出等价的Class D的代码(尽可能地相近),如何获得像D一样的效果与可用性,且在客户端的语法如何进行最小的改变。

开始:你要开始考虑在以下代码的情形里:

void f1( A&  x ) { cout << "f1:" << x.Name() << endl; }
void f2( B1& x ) { cout << "f2:" << x.Name() << endl; }
void f3( B2& x ) { cout << "f3:" << x.Name() << endl; }

void g1( A   x ) { cout << "g1:" << x.Name() << endl; }
void g2( B1  x ) { cout << "g2:" << x.Name() << endl; }
void g3( B2  x ) { cout << "g3:" << x.Name() << endl; }

int main() {
    D   d;
    B1* pb1 = &d;   // D* -> B* 转换
    B2* pb2 = &d;
    B1& rb1 = d;    // D& -> B& 转换
    B2& rb2 = d;

    f1( d );        // 多态(polymorphism)
    f2( d );
    f3( d );

    g1( d );        // 切片(slicing)
    g2( d );
    g3( d );
                    // dynamic_cast/RTTI
    cout << ( (dynamic_cast<D*>(pb1) != 0)
            ? "ok " : "bad " );
    cout << ( (dynamic_cast<D*>(pb2) != 0)
            ? "ok " : "bad " );

    try {
        dynamic_cast<D&>(rb1);
        cout << "ok ";
    } catch(...) {
        cout << "bad ";
    }
    try {
        dynamic_cast<D&>(rb2);
        cout << "ok ";
    } catch(...) {
        cout << "bad ";
    }
} 

解决方法:

1.思考以下代码:

  struct A      { virtual ~A() { }
                  virtual string Name() { return "A";  } };
  struct B1 : virtual A { string Name() { return "B1"; } };
  struct B2 : virtual A { string Name() { return "B2"; } };

  struct D  : B1, B2    { string Name() { return "D";  } };

在以上示例中,如果不使用多重继承,如何写出等价的Class D的代码(尽可能地相近),如何获得像D一样的效果与可用性,且在客户端的语法如何进行最小的改变。

这儿有几种策略,每种都有弱点,以下这种是比较完整的:

    struct D : B1 {
        struct D2 : B2 {
            void   Set ( D* d ) { d_ = d; }
            string Name();
            D* d_;
        } d2_;

        D()                 { d2_.Set( this ); }

        D( const D& other ) : B1( other ), d2_( other.d2_ )
                            { d2_.Set( this ); }

        D& operator=( const D& other ) {
                              B1::operator=( other );
                              d2_ = other.d2_;
                              return *this;
                            }

        operator B2&()      { return d2_; }

        B2& AsB2()          { return d2_; }

        string Name()       { return "D"; }
    };
    string D::D2::Name()    { return d_->Name(); }

但有几个缺点:

-提供operator B2&引起引用结束(不一致性引起)

-你需要调用D::AsB2()来显式地使用D来像B2(在测试代码中,有一处需要改变:"B2* pb2 = &d;"改变成"B2* pb2 = &d.AsB2();")

-dynamic_cast从D*动态转换成B2*将不能工作,(如果你使用前处理来重定义dynamic_cast,它可能会工作)

有趣的是,你必须要观察D对象在内存中的布局,在多重继承中是否是相同的对象,因为我们只是尝试模仿MI,刚才没有考虑到所有语法与对语言内存提供支持的方便性。




相关文章

相关软件