Sign in to follow this  
Makubab

Multiple Inheritance Polymorphism

Recommended Posts

Makubab    113
Hello,

I came across an issue with multiple inheritance and polymorphism and isolated it down to the following code:

[CODE]
class class0
{
public:
virtual void test0(void);
};
class Class1
{
public:
virtual void test1(void);
virtual void test2(void);
};
class Class2 : public Class1, public class0
{
public:
virtual void test1(void);
virtual void test2(void);
virtual void test0(void);
};
void Class2::test0(void)
{
}
void class0::test0(void)
{
}
void Class1::test1(void)
{
}
void Class1::test2(void)
{
}
void Class2::test1(void)
{
}
void Class2::test2(void)
{
}
int main(void)
{
Class1* c1 = new Class2[10];
// Works
((Class2*)c1)[1].test1();
((Class2*)c1)[1].test2();

// Does not work.
c1[1].test1();
c1[1].test2();
delete[] c1;
}
[/CODE]

Now, it seems to me that under polymorphism, both those cases should work. When I investigate c1's virtual function table, it changes at each index. Various testing has shown me the virtual function table ranges from being just plain wrong to not showing up per item correctly. For example it will show only Class1's table for c1[0], and only class0's table for c1[1]. The code that does not work will crash due to an Access Violation. This is with Visual Studio 2010, default compiler.

Maybe I am doing something wrong, this just looks like a bug though. It seems to effectively kill the ability to do polymorphism with multiple inheritance. Any explanation on what's happening here or if I am missing something important would be appreciated.

Thanks!

Share this post


Link to post
Share on other sites
wqking    761
The problem is
Class1* c1 = new Class2[10];

C1 points to an VALUE array of class2, but note Class1 and Class2 have different size.
So C1[1] won't point to correct Class2, it points to invalid memory.

You should not assign "new Class2[10]" to Class1 pointer. You need to assign to Class2 pointer.

Share this post


Link to post
Share on other sites
wqking    761
Plus, polymorphism is obtained via pointer or reference, not via value.
So to get polymorphism, you need to allocate for pointers.
Pseudo code,

Class1 ** c1 = new Class1 * [10];
for(loop 10) C1[i] = new Class2;

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this