This is the mail archive of the gcc-bugs@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: Deleting a virtual base class pointer gives segmentation fault.



> Your base class Alfa is not polymorphic, therefore it won't work.
> You need to either
> 1) make Alfa polymorphic (add virtual functions, probably the dtor)
> 2) make a and Alfa_i pointer.

Ah, thank you!
A virtual destructor in Alfa does the trick:

struct Alfa
{
        virtual ~Alfa () {};        
};

struct Alfa_i : public virtual  Alfa
{
};

int main ()
{
        Alfa *a = new Alfa_i;
        delete a;
}

It is only the virtual desctructor that makes this possible. In my
original code I have many virtual functions in Alfa, but no
destructor since Alfa is only used as an interface class. I would
like to avoid adding real code to an interface class and because of
that I tried to following code:

struct Alfa
{
        virtual ~Alfa () = 0;
};

It compiles, but fails at linking. Maybe a warning should be emitted
for this code, or maybe not.....

Regards
Fredrik Öhrström





Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]