SlideShare a Scribd company logo
[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

More Related Content

Similar to C++ diamond problem

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 剖析與實踐
KAI CHU CHUNG
 
Become an xcoder
Become an xcoder Become an xcoder
Become an xcoder
Ivan lin
 
Things about Functional JavaScript
Things about Functional JavaScriptThings about Functional JavaScript
Things about Functional JavaScript
ChengHui Weng
 
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
Kouji Matsui
 
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
Sebastian Witowski
 
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
ShaiAlmog1
 
(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii(7) cpp abstractions inheritance_part_ii
(7) cpp abstractions inheritance_part_ii
Nico Ludwig
 
Practical git for developers
Practical git for developersPractical git for developers
Practical git for developers
Wim Godden
 
Boo Manifesto
Boo ManifestoBoo Manifesto
Boo Manifesto
hu hans
 
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
Nho Vĩnh
 
Javascript Apps at Build Artifacts
Javascript Apps at Build ArtifactsJavascript Apps at Build Artifacts
Javascript Apps at Build Artifacts
Clay Smith
 
GIT training - advanced for software projects
GIT training - advanced for software projectsGIT training - advanced for software projects
GIT training - advanced for software projects
Thierry Gayet
 
Geek git
Geek gitGeek 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
snowfarthing
 
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
Konrad Malawski
 
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
Nick Plante
 
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
Nicolas Bettenburg
 
Code generation
Code generationCode generation
Code generation
Rafael Chaves
 
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
Frances Coronel
 

Similar to 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
 

More from Saket Pathak

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

More from 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
 

Recently uploaded

How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
danishmna97
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Speck&Tech
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
KAMESHS29
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
Mariano Tinti
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
shyamraj55
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
Edge AI and Vision Alliance
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
Kari Kakkonen
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
Neo4j
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
Zilliz
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Paige Cruz
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
Quotidiano Piemontese
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
Neo4j
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
Neo4j
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
Matthew Sinclair
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
名前 です男
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
Safe Software
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
DianaGray10
 

Recently uploaded (20)

How to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptxHow to Get CNIC Information System with Paksim Ga.pptx
How to Get CNIC Information System with Paksim Ga.pptx
 
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
Cosa hanno in comune un mattoncino Lego e la backdoor XZ?
 
RESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for studentsRESUME BUILDER APPLICATION Project for students
RESUME BUILDER APPLICATION Project for students
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Mariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceXMariano G Tinti - Decoding SpaceX
Mariano G Tinti - Decoding SpaceX
 
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with SlackLet's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
Let's Integrate MuleSoft RPA, COMPOSER, APM with AWS IDP along with Slack
 
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
“Building and Scaling AI Applications with the Nx AI Manager,” a Presentation...
 
Climate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing DaysClimate Impact of Software Testing at Nordic Testing Days
Climate Impact of Software Testing at Nordic Testing Days
 
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
GraphSummit Singapore | The Future of Agility: Supercharging Digital Transfor...
 
Infrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI modelsInfrastructure Challenges in Scaling RAG with Custom AI models
Infrastructure Challenges in Scaling RAG with Custom AI models
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfObservability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdf
 
National Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practicesNational Security Agency - NSA mobile device best practices
National Security Agency - NSA mobile device best practices
 
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
GraphSummit Singapore | Neo4j Product Vision & Roadmap - Q2 2024
 
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024GraphSummit Singapore | The Art of the  Possible with Graph - Q2 2024
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 
20240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 202420240609 QFM020 Irresponsible AI Reading List May 2024
20240609 QFM020 Irresponsible AI Reading List May 2024
 
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
みなさんこんにちはこれ何文字まで入るの?40文字以下不可とか本当に意味わからないけどこれ限界文字数書いてないからマジでやばい文字数いけるんじゃないの?えこ...
 
Essentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FMEEssentials of Automations: The Art of Triggers and Actions in FME
Essentials of Automations: The Art of Triggers and Actions in FME
 
Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1Communications Mining Series - Zero to Hero - Session 1
Communications Mining Series - Zero to Hero - Session 1
 

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 />