SlideShare a Scribd company logo
2
Most read
[Year]Saket Kr. PathakSoftware Developer2d/3d Graphics<br />[ C++  Diamond Problem ] Discussion, concern to the Diamond Problem of Multiple Inheritance in C++.<br />Two friends taking burger to the common.<br />Hello all my mate... few days back, one of my friend close to my heart because of this Qs. :) asked me ... in a tea stall (the gathering place near my home, at evening after returning back from Office) ... <br />Yup, unfortunately after days ... today I got time to read a bit more and trying to represent my view ... <br />Let's see a code-snippet ... <br />//Base class<br />class Burger<br />{<br />public:<br />  Burger(int); //Simple parameterized constructor<br />  virtual ~Burger();//Normal Override Destruct-or to avoid memory leak<br />  virtual void McVeggie();//General Virtual Function to override in Derived Classes<br />  virtual void cafeMocha();//General Virtual Function to override in Derived Classes<br />}; <br />//Derived class - 1st<br />class Guy1   :   public Burger<br />{<br /> public:<br />    Guy1(int); //Simple parameterized constructor<br />    virtual ~Guy1();//Normal Override Destruct-or to avoid memory leak<br />    virtual void McVeggie();//General Virtual Function to override in Derived Classes<br />    virtual void cafeMocha();             //General Virtual Function to override in Derived Classes<br />};<br /> <br />//Derived class - 2nd <br />class Guy2   :   public Burger<br />{<br /> public:<br />    Guy2(int);//Simple parameterized constructor<br />    virtual ~Guy2();//Normal Override Destruct-or to avoid memory leak<br />    virtual void McVeggie();//General Virtual Function to override in Derived Classes<br />    virtual void cafeMocha();//General Virtual Function to override in Derived Classes<br />};<br /> <br /> <br />//Derived class – 3rd<br />class CommonFriend   :   public Guy1, public Guy2<br />{<br /> public:<br />    CommonFriend();//Simple parameterized constructor<br />    virtual ~CommonFriend();//Normal Override Destruct-or to avoid memory leak<br />};<br /> <br />Now when we call a burger of either type for the common friend like;<br /> <br />int main()<br />{<br />            CommonFriend needBurger;<br />            needBurger.McVeggie();                                 //Error type Ambiguous<br />            needBurger.cafeMocha();                               //Error type Ambiguous<br /> <br />            return 0;<br />}<br /> <br />This will create Compiler Error for \"
Ambiguous Type\"
. Now why ... ???<br />As we all know through the concept of overriding in inheritance we simple put a new definition for the function having same signature from base class, these are maintained by compiler by providing virtual function table (vtable) for classes Guy1 and Guy2. <br />But here in 3rd level of inheritance that is indeed multiple inheritances, the provided virtual function table has the pointer for both Guy1 and Guy2 classes which contains their respective pointer of Burger. <br />As the result when we are calling for Mc.Veggie and cafeMocha, the compiler gets ambiguous to make it happen. To avoid this we can do it ... we need to do ...<br />//Derived class - 1st <br />class Guy1   :   public virtual Burger<br />{<br />            public:<br />                     //Same as above<br />}; <br />//Derived class - 2nd <br />class Guy2   :   public virtual Burger<br />{<br />            public:                     <br />                     //Same as above<br />};<br /> <br />Now when the virtual function table for CommonFriend will be created then Compiler will mind the above structure and keep single instance of all the above base classes i.e Bueger, Guy1, Guy2. <br />That's all I think we need to do with multiple inheritance and Diamond problem. <br />Yes people may surprise with the naming conventions of class, functions and instances. It's my choice I had tried to represent the problem in layman terms that will be easily imaginable as well as understandable instead of tricky words or random Letters ... :) that's my way ... yup I will expect comments from you C++ buddies ... so welcome ... :)<br /> <br />References:<br />http://en.wikipedia.org/wiki/Multiple_inheritance<br />http://www.cprogramming.com/tutorial/virtual_inheritance.html<br />http://www.cs.cmu.edu/~donna/public/malayeri.TR08-169.pdf<br />
C++ diamond problem
C++ diamond problem
C++ diamond problem
C++ diamond problem

More Related Content

PDF
Palestra "Do PHP ao Rails" - FOCAI
ODP
Chef - the second presentation
PDF
C++ progrmming language
PPTX
Uid
PPTX
Virtual base class
PPTX
Multiple inheritance in c++
PPTX
Multiple inheritance
PPT
Multiple Inheritance powerpoint presentation
Palestra "Do PHP ao Rails" - FOCAI
Chef - the second presentation
C++ progrmming language
Uid
Virtual base class
Multiple inheritance in c++
Multiple inheritance
Multiple Inheritance powerpoint presentation

Similar to C++ diamond problem (11)

PPTX
Inheritance in c++ by Manan Pasricha
PPSX
Inheritance and Polymorphism in Oops
PPTX
Inheritance in c++
PDF
C++ Multiple Inheritance
PPTX
Inheritance
PPTX
Aryan's pres. entation.pptx
PPTX
inheritance in OOPM
PPTX
Object Oriented Design and Programming Unit-03
DOC
How would you implement multiple inheritance in java
PDF
c++Inheritance.pdf
PPTX
Introduction to inheritance and different types of inheritance
Inheritance in c++ by Manan Pasricha
Inheritance and Polymorphism in Oops
Inheritance in c++
C++ Multiple Inheritance
Inheritance
Aryan's pres. entation.pptx
inheritance in OOPM
Object Oriented Design and Programming Unit-03
How would you implement multiple inheritance in java
c++Inheritance.pdf
Introduction to inheritance and different types of inheritance
Ad

More from Saket Pathak (13)

DOCX
Wan Important Questions
DOC
Wan notes
DOCX
C++ lab assignment
DOCX
Data Structure in C (Lab Programs)
DOCX
A Guy and gal in STL
DOCX
Lab. Programs in C
DOCX
GNU GCC - what just a compiler...?
PDF
C++ friendship
DOCX
C++ Template
DOC
Copy constructor
DOCX
Malloc, calloc, realloc
PPT
Recursion in c
PDF
Pointers in c
Wan Important Questions
Wan notes
C++ lab assignment
Data Structure in C (Lab Programs)
A Guy and gal in STL
Lab. Programs in C
GNU GCC - what just a compiler...?
C++ friendship
C++ Template
Copy constructor
Malloc, calloc, realloc
Recursion in c
Pointers in c
Ad

Recently uploaded (20)

PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
Enhancing emotion recognition model for a student engagement use case through...
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
Web App vs Mobile App What Should You Build First.pdf
PDF
NewMind AI Weekly Chronicles – August ’25 Week III
PDF
A contest of sentiment analysis: k-nearest neighbor versus neural network
PPTX
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
Tartificialntelligence_presentation.pptx
PDF
Architecture types and enterprise applications.pdf
PDF
A novel scalable deep ensemble learning framework for big data classification...
PDF
Assigned Numbers - 2025 - Bluetooth® Document
PDF
WOOl fibre morphology and structure.pdf for textiles
PDF
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
A comparative study of natural language inference in Swahili using monolingua...
PPT
What is a Computer? Input Devices /output devices
PDF
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
PPTX
Modernising the Digital Integration Hub
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
Enhancing emotion recognition model for a student engagement use case through...
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
Web App vs Mobile App What Should You Build First.pdf
NewMind AI Weekly Chronicles – August ’25 Week III
A contest of sentiment analysis: k-nearest neighbor versus neural network
TechTalks-8-2019-Service-Management-ITIL-Refresh-ITIL-4-Framework-Supports-Ou...
Zenith AI: Advanced Artificial Intelligence
Tartificialntelligence_presentation.pptx
Architecture types and enterprise applications.pdf
A novel scalable deep ensemble learning framework for big data classification...
Assigned Numbers - 2025 - Bluetooth® Document
WOOl fibre morphology and structure.pdf for textiles
2021 HotChips TSMC Packaging Technologies for Chiplets and 3D_0819 publish_pu...
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
A comparative study of natural language inference in Swahili using monolingua...
What is a Computer? Input Devices /output devices
Profit Center Accounting in SAP S/4HANA, S4F28 Col11
Modernising the Digital Integration Hub
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf

C++ diamond problem

  • 1. [Year]Saket Kr. PathakSoftware Developer2d/3d Graphics<br />[ C++ Diamond Problem ] Discussion, concern to the Diamond Problem of Multiple Inheritance in C++.<br />Two friends taking burger to the common.<br />Hello all my mate... few days back, one of my friend close to my heart because of this Qs. :) asked me ... in a tea stall (the gathering place near my home, at evening after returning back from Office) ... <br />Yup, unfortunately after days ... today I got time to read a bit more and trying to represent my view ... <br />Let's see a code-snippet ... <br />//Base class<br />class Burger<br />{<br />public:<br /> Burger(int); //Simple parameterized constructor<br />  virtual ~Burger();//Normal Override Destruct-or to avoid memory leak<br />  virtual void McVeggie();//General Virtual Function to override in Derived Classes<br />  virtual void cafeMocha();//General Virtual Function to override in Derived Classes<br />}; <br />//Derived class - 1st<br />class Guy1   :   public Burger<br />{<br /> public:<br />    Guy1(int); //Simple parameterized constructor<br />    virtual ~Guy1();//Normal Override Destruct-or to avoid memory leak<br />    virtual void McVeggie();//General Virtual Function to override in Derived Classes<br />    virtual void cafeMocha();             //General Virtual Function to override in Derived Classes<br />};<br /> <br />//Derived class - 2nd <br />class Guy2   :   public Burger<br />{<br /> public:<br />    Guy2(int);//Simple parameterized constructor<br />    virtual ~Guy2();//Normal Override Destruct-or to avoid memory leak<br />    virtual void McVeggie();//General Virtual Function to override in Derived Classes<br />    virtual void cafeMocha();//General Virtual Function to override in Derived Classes<br />};<br /> <br /> <br />//Derived class – 3rd<br />class CommonFriend   :   public Guy1, public Guy2<br />{<br /> public:<br />    CommonFriend();//Simple parameterized constructor<br />    virtual ~CommonFriend();//Normal Override Destruct-or to avoid memory leak<br />};<br /> <br />Now when we call a burger of either type for the common friend like;<br /> <br />int main()<br />{<br />            CommonFriend needBurger;<br />            needBurger.McVeggie();                                 //Error type Ambiguous<br />            needBurger.cafeMocha();                               //Error type Ambiguous<br /> <br />            return 0;<br />}<br /> <br />This will create Compiler Error for \" Ambiguous Type\" . Now why ... ???<br />As we all know through the concept of overriding in inheritance we simple put a new definition for the function having same signature from base class, these are maintained by compiler by providing virtual function table (vtable) for classes Guy1 and Guy2. <br />But here in 3rd level of inheritance that is indeed multiple inheritances, the provided virtual function table has the pointer for both Guy1 and Guy2 classes which contains their respective pointer of Burger. <br />As the result when we are calling for Mc.Veggie and cafeMocha, the compiler gets ambiguous to make it happen. To avoid this we can do it ... we need to do ...<br />//Derived class - 1st <br />class Guy1   :   public virtual Burger<br />{<br />            public:<br />                     //Same as above<br />}; <br />//Derived class - 2nd <br />class Guy2   :   public virtual Burger<br />{<br />            public:                     <br />                     //Same as above<br />};<br /> <br />Now when the virtual function table for CommonFriend will be created then Compiler will mind the above structure and keep single instance of all the above base classes i.e Bueger, Guy1, Guy2. <br />That's all I think we need to do with multiple inheritance and Diamond problem. <br />Yes people may surprise with the naming conventions of class, functions and instances. It's my choice I had tried to represent the problem in layman terms that will be easily imaginable as well as understandable instead of tricky words or random Letters ... :) that's my way ... yup I will expect comments from you C++ buddies ... so welcome ... :)<br /> <br />References:<br />http://en.wikipedia.org/wiki/Multiple_inheritance<br />http://www.cprogramming.com/tutorial/virtual_inheritance.html<br />http://www.cs.cmu.edu/~donna/public/malayeri.TR08-169.pdf<br />