SlideShare ist ein Scribd-Unternehmen logo
1 von 5
[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 quot;
Ambiguous Typequot;
. 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

Weitere ähnliche Inhalte

Ähnlich wie C++ diamond problem

Ähnlich wie C++ diamond problem (20)

Coscup x ruby conf tw 2021 google cloud buildpacks 剖析與實踐
Coscup x ruby conf tw 2021  google cloud buildpacks 剖析與實踐Coscup x ruby conf tw 2021  google cloud buildpacks 剖析與實踐
Coscup x ruby conf tw 2021 google cloud buildpacks 剖析與實踐
 
Become an xcoder
Become an xcoder Become an xcoder
Become an xcoder
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
 
Making archive IL2C #6-55 dotnet600 2018
Making archive IL2C #6-55 dotnet600 2018Making archive IL2C #6-55 dotnet600 2018
Making archive IL2C #6-55 dotnet600 2018
 
5 Things I Wish I Knew About Gitlab CI
5 Things I Wish I Knew About Gitlab CI5 Things I Wish I Knew About Gitlab CI
5 Things I Wish I Knew About Gitlab CI
 
Hacking the Codename One Source Code - Part V - Transcript.pdf
Hacking the Codename One Source Code - Part V - Transcript.pdfHacking the Codename One Source Code - Part V - Transcript.pdf
Hacking the Codename One Source Code - Part V - Transcript.pdf
 
(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii
 
Practical git for developers
Practical git for developersPractical git for developers
Practical git for developers
 
Boo Manifesto
Boo ManifestoBoo Manifesto
Boo Manifesto
 
Go Programming by Example_ Nho Vĩnh Share.pdf
Go Programming by Example_ Nho Vĩnh Share.pdfGo Programming by Example_ Nho Vĩnh Share.pdf
Go Programming by Example_ Nho Vĩnh Share.pdf
 
Javascript Apps at Build Artifacts
Javascript Apps at Build ArtifactsJavascript Apps at Build Artifacts
Javascript Apps at Build Artifacts
 
GIT training - advanced for software projects
GIT training - advanced for software projectsGIT training - advanced for software projects
GIT training - advanced for software projects
 
Geek git
Geek gitGeek git
Geek git
 
How to debug the pod which is hard to debug (디버그 하기 어려운 POD 디버그 하기)
How to debug the pod which is hard to debug (디버그 하기 어려운 POD 디버그 하기)How to debug the pod which is hard to debug (디버그 하기 어려운 POD 디버그 하기)
How to debug the pod which is hard to debug (디버그 하기 어려운 POD 디버그 하기)
 
A Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and AllegroA Simple 3D Graphics Engine Written in Python and Allegro
A Simple 3D Graphics Engine Written in Python and Allegro
 
The things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and AkkaThe things we don't see – stories of Software, Scala and Akka
The things we don't see – stories of Software, Scala and Akka
 
Building native Android applications with Mirah and Pindah
Building native Android applications with Mirah and PindahBuilding native Android applications with Mirah and Pindah
Building native Android applications with Mirah and Pindah
 
Studying the impact of Social Structures on Software Quality
Studying the impact of Social Structures on Software QualityStudying the impact of Social Structures on Software Quality
Studying the impact of Social Structures on Software Quality
 
Code generation
Code generationCode generation
Code generation
 
Fullstack Academy - Awesome Web Dev Tips & Tricks
Fullstack Academy - Awesome Web Dev Tips & TricksFullstack Academy - Awesome Web Dev Tips & Tricks
Fullstack Academy - Awesome Web Dev Tips & Tricks
 

Mehr von Saket Pathak

Mehr von Saket Pathak (13)

Wan Important Questions
Wan Important QuestionsWan Important Questions
Wan Important Questions
 
Wan notes
Wan notesWan notes
Wan notes
 
C++ lab assignment
C++ lab assignmentC++ lab assignment
C++ lab assignment
 
Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)Data Structure in C (Lab Programs)
Data Structure in C (Lab Programs)
 
A Guy and gal in STL
A Guy and gal in STLA Guy and gal in STL
A Guy and gal in STL
 
Lab. Programs in C
Lab. Programs in CLab. Programs in C
Lab. Programs in C
 
GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?GNU GCC - what just a compiler...?
GNU GCC - what just a compiler...?
 
C++ friendship
C++ friendshipC++ friendship
C++ friendship
 
C++ Template
C++ TemplateC++ Template
C++ Template
 
Copy constructor
Copy constructorCopy constructor
Copy constructor
 
Malloc, calloc, realloc
Malloc, calloc, reallocMalloc, calloc, realloc
Malloc, calloc, realloc
 
Recursion in c
Recursion in cRecursion in c
Recursion in c
 
Pointers in c
Pointers in cPointers in c
Pointers in c
 

Kürzlich hochgeladen

Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Victor Rentea
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
?#DUbAI#??##{{(☎️+971_581248768%)**%*]'#abortion pills for sale in dubai@
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Victor Rentea
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
WSO2
 

Kürzlich hochgeladen (20)

Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
Modular Monolith - a Practical Alternative to Microservices @ Devoxx UK 2024
 
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
+971581248768>> SAFE AND ORIGINAL ABORTION PILLS FOR SALE IN DUBAI AND ABUDHA...
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...Apidays New York 2024 - The value of a flexible API Management solution for O...
Apidays New York 2024 - The value of a flexible API Management solution for O...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
Apidays New York 2024 - The Good, the Bad and the Governed by David O'Neill, ...
 
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
Apidays New York 2024 - APIs in 2030: The Risk of Technological Sleepwalk by ...
 
Architecting Cloud Native Applications
Architecting Cloud Native ApplicationsArchitecting Cloud Native Applications
Architecting Cloud Native Applications
 
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
ICT role in 21st century education and its challenges
ICT role in 21st century education and its challengesICT role in 21st century education and its challenges
ICT role in 21st century education and its challenges
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
Biography Of Angeliki Cooney | Senior Vice President Life Sciences | Albany, ...
 
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data DiscoveryTrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
TrustArc Webinar - Unlock the Power of AI-Driven Data Discovery
 

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 quot; Ambiguous Typequot; . 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 />