This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
Re: Deleting a virtual base class pointer gives segmentation fault.
- To: nathan at cs dot bris dot ac dot uk
- Subject: Re: Deleting a virtual base class pointer gives segmentation fault.
- From: Fredrik Öhrström <d92-foh at nada dot kth dot se>
- Date: Mon, 11 Oct 1999 17:18:53 +0200 (MET DST)
- cc: gcc-bugs at gcc dot gnu dot org
> 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