Skip to main content
gamedev.net
🔒 Locked

Assert when using a virtual base class

Started by Plasmadog May 11, 2004 at 7:01 PM 7 replies 1.1k views
Original Post
Plasmadog
Plasmadog
Hi all. I''m using a virtual base class for the first time (in C++, VC7), and it has created a problem that I just don''t understand. I have stripped the code down to the absolute minimum in an effort to track it down:
class A
{};

class B : virtual public A
{};

class C : public B
{};

main()
{
   A *foo = new C;
   // do stuff

   delete foo;
}
I am getting a debug assert when I delete foo. Can anyone explain why this would be happening? Also, while reading about virtual base classes I have seen examples that look like this:
class foo : virtual public bar
{}; 
and some that look like this:
class foo : public virtual bar
{}; 
Is this just a matter of style preference, or is there a functional difference between "virtual public" and "public virtual"?
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
Sneftel
Sneftel
I''m guessing that you''ve failed to provide a virtual destructor in class A. Read the C++ FAQ-LITE for more info on this.


"Sneftel is correct, if rather vulgar." --Flarelocke
Chris Hare
Chris Hare
public virtual is the same as virtual public.


blah blah blah.
Chris Hare
Chris Hare
Interestingly enough, or not, virtual inheritence is only useful when multiple inheritence is used, which is rarely because of the associated horror. If not deriving from 2 or more base objects, removing the virtual keyword will not change execution, but will make it easier to read and comprehend.


blah blah blah.
Chris Hare
Chris Hare
Hey Sneftel, I''m nearing 300 posts in just two years! Can you beat that?


blah blah blah.
Sneftel
Sneftel
quote:
Original post by Chris Hare
Hey Sneftel, I''m nearing 300 posts in just two years! Can you beat that?

Yeh, I think I might be a couple over that.


"Sneftel is correct, if rather vulgar." --Flarelocke
Xai
Xai
i''ve read 300 sneftel posts this year alone
SiCrane
SiCrane
quote:
Original post by Chris Hare
Interestingly enough, or not, virtual inheritence is only useful when multiple inheritence is used, which is rarely because of the associated horror.



That depends on your definition of useful. Virtual inheritance can be used in certain degenerate cases of single inheritance to create classes that cannot be derived from without altering the ways that objects of that class may be created.
Plasmadog
Plasmadog
quote:
Original post by Sneftel
I'm guessing that you've failed to provide a virtual destructor in class A. Read the C++ FAQ-LITE for more info on this.



It occurred to me that the wrong destructor was being called, but I was under the impression that all destructors are virtual. Guess I was wrong. Thanks for the tip.


quote:
Original post by Chris Hare Interestingly enough, or not, virtual inheritence is only useful when multiple inheritence is used, which is rarely because of the associated horror. If not deriving from 2 or more base objects, removing the virtual keyword will not change execution, but will make it easier to read and comprehend.


I know. Like I said, I stripped the code down to bare essentials to find/illustrate the problem. I'm using virtual inheritance because the classes involved will be part of a larger system in which a diamond inheritance pattern will occur.

[edited by - Plasmadog on May 11, 2004 11:25:20 PM]
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.