Sign in to follow this  
Sutekh

Virtual inheritance and dynamic_cast

Recommended Posts

Sutekh    122
I am toying around with some code from an article in this month’s Dr Dobb’s. It requires using virtual inheritance to provide an abstraction to templated classes. My problem is that VS2k5, doesn’t like virtual inheritance from empty classes, or classes with no virtual functions. Is this proper behavior for the compiler? edit: The error I get is: error C2683: 'dynamic_cast' : 'A' is not a polymorphic type
class A
{};

class B : public virtual A
{
public:
	void output()
	{
		std::cout << "test" << std::endl;
	}
};

int main()
{

	A* a = new B;
	B* b = dynamic_cast< B* > (a);
	b->output();

  system( "pause" );
    return 0;
}



Share this post


Link to post
Share on other sites
Fruny    1658
A type is polymorphic if and only if it has a virtual function. Virtual inheritance have very little to do with that. Additionally, most uses of polymorphic object also require a virtual destructor.

This is perfectly correct behavior, yes. dynamic_cast relies on run-time type information that is typically only generated (and logically so) for polymorphic types.

Share this post


Link to post
Share on other sites
Sutekh    122
Quote:
Original post by Fruny
A type is polymorphic if and only if it has a virtual function. Virtual inheritance have very little to do with that. Additionally, most uses of polymorphic object also require a virtual destructor.

This is perfectly correct behavior, yes. dynamic_cast relies on run-time type information that is typically only generated (and logically so) for polymorphic types.


Thanks for the quick response. So virtual inheritance's only purpose is to prevent a derived class from inheriting the same base class more than once? Is there no way to create an empty base class that is polymorphic?

Share this post


Link to post
Share on other sites
Shinkage    595
Quote:
Original post by Sutekh
Thanks for the quick response. So virtual inheritance's only purpose is to prevent a derived class from inheriting the same base class more than once? Is there no way to create an empty base class that is polymorphic?


Yeah, that's basically the long and the short of what virtual inheritance is for. As for a polymorphic base class, you could make a class with only an empty virtual destructor. If you get lucky your compiler might even optimize it away.

class base_class
{
virtual ~base_class() {}
};


Since you're likely to be trying to delete derived classes from pointers to the base in the sort of scheme you're using anyway, you'll have to give your base a virtual destructor either way to make sure it works right. Remember, in the following example, derived_class' destructor will NOT be called properly.

class base_class {};

class derived_class : public base_class
{
~derived_class()
{
... do some stuff here ...
}
};

...

base_class *obj = new derived_class;
delete obj;


Hence the need for virtual destructors.

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