Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Question about multi-inheritance in C++?

I have the following code:

#include "stdafx.h"
#include <iostream>
#include <conio.h>
using namespace std;
#define MNAME 30
class Person {
public:
    char name[MNAME + 1];
};
class Student : public Person {
};
class Staff : public Person {
};
class Faculty : public Student, public Staff {
};

int _tmain(int argc, _TCHAR* argv[])
{
    Faculty faculty;
    cout << "Address of faculty.Person::name: " << &faculty.Person::name << endl;
    cout << "Address of faculty.Student::name: " << &faculty.Student::name << endl;
    cout << "Address of faculty.Staff::name: " << &faculty.Staff::name << endl;

    getch();
    return 0;
}

When executed, the program gives the results:

Address of faculty.Person::name: 0012FF20 // **Line 1**
Address of faculty.Student::name: 0012FF20 // **Line 2**
Address of faculty.Staff::name: 0012FF3F // **Line 3**

I don't get it. Why the address in Line 1 and Line 2 is different from Line 3, while both Student and Staff inherits name from Person?

like image 576
ipkiss Avatar asked May 18 '11 11:05

ipkiss


People also ask

What problem can be caused by multiple inheritance?

Multiple inheritance has been a controversial issue for many years, with opponents pointing to its increased complexity and ambiguity in situations such as the "diamond problem", where it may be ambiguous as to which parent class a particular feature is inherited from if more than one parent class implements said ...

How does C support multiple inheritance?

Master C and Embedded C Programming- Learn as you go Multiple inheritance occurs when a class inherits from more than one base class. So the class can inherit features from multiple base classes using multiple inheritance. This is an important feature of object oriented programming languages such as C++.

Which operator is used during multiple inheritance?

The ambiguity is shown by the derived class object which invokes one of the same-named functions. Inorder to resolve the ambiguity problem in multiple inheritance we use the resolution '::' operator along with which we specify the class name from which the member function is to be invoked.

When multiple inheritance concept should be used?

Multiple inheritance is useful when a subclass needs to combine multiple contracts and inherit some, or all, of the implementation of those contracts. For example, the AmericanStudent class needs to inherit from both the Student class and the American class.


2 Answers

When you do multiple inheritance this way you get two copies of the grandparent class. This is the classic dreaded diamond problem, where you try to do this:


        Person
       /       \
     Student  Staff
       \       /
        Faculty

but through normal inheritance you actually get this:

    Person   Person
      |        |
     Student Staff
       \       /
        Faculty

So there's really 2 Person's in an instance of Faculty, meaning youll get 2 names.

To get the diamond in the first diagram above, you want to use virtual inheritance.

class Staff : public virtual Person {
};
class Student : public virtual Person {
};
like image 186
Doug T. Avatar answered Sep 19 '22 13:09

Doug T.


With regular multiple inheritance you get multiple copies of shared base classes. If you want one copy, use virtual inheritance.

Explained well in Wikipedia

class Student : public virtual Person {
};
class Staff : public virtual Person {
};

Will get you what you expected

like image 27
Lou Franco Avatar answered Sep 17 '22 13:09

Lou Franco