Re: why compile error in multiple inheritance
Tony Johansson wrote:[color=blue]
> I have been playing around a bit with this multiple inheritance and I
> wonder why do I get a compile error
> if I remove the virtual keyvord from the declaration of the Student
> class and the Employee class.
> I have removed all kind of data from all classes.
> The compile error occur when I instansiate Person* p = new
> TeachingAssistent;
> The error I get is c:\Documents and
> Settings\Tony\kau\cplusplus\test4\start.cpp(8): error C2594:
> 'initializing'[color=green]
>> ambiguous conversions from 'TeachingAssistent *' to 'Person *'[/color]
>
> I know that if I have some data in the virtual base klass and not
> using the virtual keyword[/color]
How could you have a virtual base class without using 'virtual' keyword?
[color=blue]
> I would get compile error because of having
> double defined data in the TeachingAssistent() but I understand why
> so that's is no problem.[/color]
Since your 'TeachingAssistent' (BTW, in English it's spelled slightly
differently: 'TeachingAssistant') has _two_ copies of 'Person' class,
one from 'Student', and the other from 'Employee', the compiler cannot
decide to which 'Person' to convert the pointer to 'TeachingAssistent'.
[color=blue]
> #include <string>
> using namespace std;
>
> class Person
> {
> public:
> Person() {}
> string getName() const
> {
> return "rulle";
> }
> };
>
> class Student : public Person
> {
> public:
> Student() {}
> };
>
> class Employee : public Person
> {
> public:
> Employee() {}
> };
>
> class TeachingAssistent : public Student, public Employee
> {
> public:
> TeachingAssistent() {}
> };
>
> class GraduateAssistent : public TeachingAssistent
> {
> };
>
> int main()
> {
> Person* p = new TeachingAssistent;
> cout << p->getName() << endl;
> }[/color]
V |