C++ Inheritance Flashcards
In multiple inheritance, in what order are the constructors called?
In the same order that they are declared in within the declaration of the calling class.
What is the Diamond Problem?
An ambiguous situation caused by multiple inheritance. It occurs when a class inherits from 2 other classes, both of which inherit from a common base class. Thus, when the bottom class tries to access a member of the base class, it does not though which class to inherit the member from.
What are the names of the 2 potential solutions to the Diamond Problem?
Solution 1: Use the scope operator
Solution 2: Use Virtual Inheritance
Describe how the Scope Operator can resolve the Diamond Problem…
The derived instance can specify through which class is wants to inherit the base class member.
Describe how Virtual Inheritance can resolve the Diamond Problem…
Prevents multiple copies of the Common Base Class being copied to the Derived Class. Thus, only 1 copy of the base class is shared with the derived classes. This removes ambiguity.
How does Virtual Inheritance work?
When multiple classes virtually inherit a common base class, only 1 copy of the common base class is inherited across all the derived classes. Thus there is no ambiguity when a derived class attempts to access a member of the base class, since all derived classes have the same copy.
What does the umbrella term of Virtual Members consist of?
Virtual Classes + Virtual Functions.
What causes ambiguity in inheritance?
When a class derives multiple classes, each of which have their own assignment of a variable of the same name. Thus, when the derived class calls the variable, it doesn’t know from which class to call the variable from.
Provide an implementation of the Diamond Problem. Then resolve it using Virtual Inheritance…
…