码迷,mamicode.com
首页 > 其他好文 > 详细

Diamond-Problem

时间:2014-08-30 15:13:50      阅读:232      评论:0      收藏:0      [点我收藏+]

标签:style   http   color   os   io   java   strong   ar   for   

The_diamond_problem (钻石问题)

http://en.wikipedia.org/wiki/Multiple_inheritance#The_diamond_problem

The "diamond problem" (sometimes referred to as the "deadly diamond of death"[6]) is an ambiguity that arises when two classes B and C inherit from A, and class D inherits from both B and C. If there is a method in A that B and/or C has overridden, and D does not override it, then which version of the method does D inherit: that of B, or that of C?

For example, in the context of GUI software development, a classButtonmay inherit from both classesRectangle(for appearance) andClickable(for functionality/input handling), and classesRectangleandClickableboth inherit from theObjectclass. Now if theequalsmethod is called for aButtonobject and there is no such method in theButtonclass but there is an overriddenequalsmethod inRectangleorClickable(or both), which method should be eventually called?

It is called the "diamond problem" because of the shape of the class inheritance diagram in this situation. In this case, class A is at the top, both B and C separately beneath it, and D joins the two together at the bottom to form a diamond shape.

bubuko.com,布布扣bubuko.com,布布扣


Avoid the Diamond Problem when Using Inheritance in Loops

http://www.devx.com/tips/Tip/32035

While multiple inheritance is an extremely useful feature—escpecially in large scale software, it can also lead to a problem known as the DDD, or "Dreadful Diamond on Derivation," or simply "The Diamond Problem," shown in the following code:

class ElectricAppliance{
  int voltage,
  public:
  int getVoltage ( ) const { return voltage; }
} ;

class Radio : public ElectricAppliance { / * ...* / } ;

class Tape : public ElectricAppliance { / * ...* / } ;

class RadioTape: public Radio, public Tape { / * ..* / } ;

int main( )
{
 RadioTape rt ;
 int voltage = rt.getVoltage( ) ;
  /* Error - ambiguous call. Two  copies getVoltage( ) exist in rt*/
}
RadioTape is derived simultaneously from two base classes, each of which has its own copy of the methods and data members of ElectricAppliance. Consequently, the object rt has two ElectricAppliance subobjects. In cases like this, where you need to reduplicate data methods from a common base class, you should use virtual inheritance:
class Radio : virtual public ElectricAppliance { / * ..* / } ;
class Tape : virtual public ElectricAppliance { / * ..* / } ;
class RadioTape: public Radio, public Tape { / * ..* / }
Now, class RadioTape contains a single shared instance of ElectricAppliance, so there are no ambiguities:
int voltage = rt .getVoltage( ) ; //OK
Note: Because of the virtual keyword in the base-class portion of Radio and Tape, an instance of ElectricAppliance will have have only a single, base subobject. This eliminates the ambiguities.


How interface resolves diamond problem in java?

Java allows the multiple inheritance of interfaces only. Interfaces are essentially abstract base classes with all abstract methods and no data members.


The diamond problem is therefore avoided since there is always only one implementation of a specific method or property and no ambiguity arises.


Diamond-Problem

标签:style   http   color   os   io   java   strong   ar   for   

原文地址:http://my.oschina.net/jerikc/blog/308312

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!