Virtual Class Inheritance
I am going to post some evil now, I will cover more on it later.
But a program I am working on has gotten to a point to where I ended up needing to do something like
[code]
class Base
{
public:
int value
};
class child1 : public Base { };
class child2 : public Base { };
class grandchild : public child1, public child2
{
public:
grandchild() { /* I want to access value here */ };
};
[/code]
Now with the above I can not access value directly I have to say which value I want since it comes down twice I have it as both child1::value and child2::value. Well I looked around to see what I can do with this problem, I found out about Virtual Base Classes so to deal with it check out the code below
[code]
class Base
{
public:
int value
};
class child1 : public Base { };
class child2 : public Base { };
class grandchild : virtual public child1, virtual public child2
{
public:
grandchild() { value };
};
[/code]
Now the above does work, the virtual keyword in the inheritance causes the grandchild object to get only one copy of the Base class so I can get to value just like if there was only one path to the Base class.
The problem I am dealing with is assume I now have a virtual function call getWhatAmI() that returns info that I can use to know what type of object I am working on, so I end up in a portion of the code where I have a grandchild object being pointed to by a Base pointer. I have an if statement that only happens if I am working with a grandchild object, now I have to cast back to a grandchild so I have access to the grandchild objects. But I can not just cast it any more I get the error of [code]
“error C2635: cannot convert a ‘Base*’ to a ‘grandchild*’; conversion from a virtual base class is implied”
[/code]
So now I have to find out how to cast with this virtual base class stuff.
I will post the solution when I find a good one.
