繁体中文
设为首页
加入收藏
当前位置:程序开发首页 >> Visual C++ >> Guru of the Week:#38 多重继承 Ⅱ.

Guru of the Week:#38 多重继承 Ⅱ.

2008-03-15 08:00:00  作者:  来源:互联网  浏览次数:0  文字大小:【】【】【
简介:/*此文是译者出于自娱翻译的GotW(Guru of the Week:http://www.gotw.ca/gotw/index.htm)系列文章的一篇,原文的版权是属于Hub Sutter(著名的C++专家,《Exceptional C++》的作者)。此文的翻译没有征得原作者的...
关键字:多重 Week Guru the of 38

/*此文是译者出于自娱翻译的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() < 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(pb1) != 0)

? "ok " : "bad " );

cout << ( (dynamic_cast(pb2) != 0)

? "ok " : "bad " );

try {

dynamic_cast(rb1);

cout << "ok ";

} catch(...) {

cout << "bad ";

}

try {

dynamic_cast(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,刚才没有考虑到所有语法与对语言内存提供支持的方便性。

责任编辑:admin
相关文章