about inheritance...

what i m wondering is is it possible to inherit two classes like

1
2
3
4
class class1: public class2 : public class3
{
    //blah blah
};


and if it is possible is it dangerous to use?

thx everyone in advance
closed account (Dy7SLyTq)
yes its possible, i cant see it being more dangerous than having single inheritance. except you do it this way:
1
2
3
4
class class1 : public class2, class3
{
     //whatevs
}
hmm thx for answering
closed account (Dy7SLyTq)
no problem. if you have any more questions dont hesitate to ask
well yeah there are some parts i have problem. but i doubt it is because i have no exp with inheritance yet.

whatever i will just ask. why i asked would it be dangerous is because what happens if both class2 and class3 has the same func name.
closed account (Dy7SLyTq)
thats called polymophism i believe. im not too advanced in oop, but i think thats it. its why the virtual keyword exists. wait no im wrong. i dont really know, but a google search unearthed this
http://stackoverflow.com/questions/2004820/inherit-interfaces-which-share-a-method-name
ok. i will read it and thx for the answer. maybe i should just sleep and tomorrow i will make a few google researches
Last edited on
is it dangerous to use?

Can be; if you run into the "diamond of death."

Andy

How can avoid the Diamond of Death in C++ if I use multiple inheritance?
http://stackoverflow.com/questions/137282/how-can-avoid-the-diamond-of-death-in-c-if-i-use-multiple-inheritance

Why should I avoid multiple inheritance in C++?
http://stackoverflow.com/questions/406081/why-should-i-avoid-multiple-inheritance-in-c
Last edited on
thx
what happens if both class2 and class3 has the same func name.
the first hides the second. you can access a specific function like so: class3::fiunc();
Actually, the compiler complains that the function is ambiguous.

(so you need to qualify with the class name to access the method.)

Also, note that this

class class1: public class2, class3

is using private inheritence for class3. As none of public, protected, or private is given, the default is used, which is private for classes (and public for structs.)

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
class class2
{
public:
    void blah_blah() {
        cout << "class2::blah_blah()" << endl;
    }
};

class class3
{
public:
    void blah_blah() {
        cout << "class3::blah_blah()" << endl;
    }
};

class class1: public class2, public class3
{
public:
    void test() {
        blah_blah();
    }
};


MSVC

1>c:\cplusplus\test\main.cpp(25) : error C2385: ambiguous access of 'blah_blah'
1>        could be the 'blah_blah' in base 'class2'
1>        or could be the 'blah_blah' in base 'class3'
1>c:\cplusplus_vc9\temp_test\main.cpp(25) : error C3861: 'blah_blah': identifier not found


GCC

main.cpp: In member function 'void class1::test()':
main.cpp:25:9: error: reference to 'blah_blah' is ambiguous
main.cpp:16:10: error: candidates are: void class3::blah_blah()
main.cpp:8:10: error:                 void class2::blah_blah()

Last edited on
thx for all your answers. i read and memorized all
Topic archived. No new replies allowed.