What does it mean to “delegate to a sister class” via virtual inheritance?
2016-01-06 09:46
387 查看
Consider the following example:
Believe it or not, when
class Base { public: virtual void foo() = 0; virtual void bar() = 0; }; class Der1 : public virtual Base { public: virtual void foo(); }; void Der1::foo() { bar(); } class Der2 : public virtual Base { public: virtual void bar(); }; class Join : public Der1, public Der2 { public: // ... }; int main() { Join* p1 = new Join(); Der1* p2 = p1; Base* p3 = p1; p1->foo(); p2->foo(); p3->foo(); }
Believe it or not, when
Der1::foo()calls
this->bar(), it ends up calling
Der2::bar(). Yes, that’s right: a class that
Der1knows nothing about will supply the override of a virtual function invoked by
Der1::foo(). This “cross delegation” can be a powerful technique for customizing the behavior of polymorphic classes.
相关文章推荐
- three.js学习笔记 基础1_1
- UE4 Hello Slate
- Spring+Hibernate声明事务
- java 中关于求素数的方法
- MFC中Unicode与多字节编码中遇到的CString与char或char*的转化问题
- MFC中Unicode与多字节编码中遇到的CString与char或char*的转化问题
- 2016-01-06 09:42:53.547:WARN:oejs.HttpChannel:qtp2109938810-27: Could not send response error 500: j
- java自动装箱拆箱总结
- 盘点即时战略游戏中高实用性寻路算法
- java自动装箱拆箱总结
- java enum(枚举)的使用
- 智能 PDU IP 地址设置步骤
- Android 使用Parcelable传递对象
- Exception in thread "main" java.lang.IllegalArgumentException:declares multiple JSON fields named id
- Android开发之Button事件实现与监听方法总结
- scheduler configuration options
- 锯齿数独 cocos2d-x 开发 (二)
- POJ-1975 Median Weight Bead(Floyed)
- java151229ExceptionTest
- Redis Cluster原理