July 10, 2011

what is DIAMOND problem in C++


Ex: Diamond Inheritance Problem in figure form.
-------------A ----------> A is the Base Class containing init()
------------/-\
-----------/---\
----------/-----\
---------B------C -------> B and C are the derived from A so.. init() is also derived
----------\-----/ both classes
-----------\---/
------------\-/
-------------D-------------> D is derived from B and C (MULTIPLE INHERITANCE), so
multiple init() method is also inherited. Here D get ambiguous state to access init() method...i.e init() from B? or init() from C?
Sol :: We can resolve this diamond problem by two ways i.e.
By using 1) By using :: (Scope resolution operator) w.r.t to the class name
2) By declaring the classes as Virtual Class.