non-virtual + virtual inheritance bug
Stuart Brooks
stuartb@cat.co.za
Mon Feb 21 17:47:00 GMT 2005
I have found what I believe to be a bug in g++ 2.91.60 on NetBSD 1.4.1. It
appears to have been solved in g++ 3.3.3 which is the other version I am
using. I was wondering if this is a known problem - a google search
revealed a few posts around this topic but nothing which matched exactly:
Basically a combination of non-virtual and virtual inheritance is resulting
in some funnies and ultimately a segmentation fault. I have narrowed it down
to the following case :
>>>
class B; // non-virtual base
class V; // virtual base
class Base: public B, public V;
class Child: public Base;
Base* b=new Child();
Child* c=dynamic_cast<Base*>(b);
passInAsBase(c);
void passInAsBase(Base* b)
{
Child* c=dynamic_cast<Child*>(b);
}
>>>
This compiles fine but causes problems when it runs (I have attached a short
compileable source file), in particular the dynamic cast in passInAsBase
segmentation faults the program. If I declare Base as follows... (virtual
class inherited first)
class Base: public V, public B;
... the problem goes away. As I mentioned this all works in 3.3.3. but
unfortunately I am tied to using my existing OS/compiler for historical
reasons.
Any pointers would be appreciated...
Thanks
Stuart
More information about the Gcc-help
mailing list