This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Segfault with delete[] operator & virtually derived classes
- From: Nathan Sidwell <nathan at codesourcery dot com>
- To: lrtaylor at micron dot com
- Cc: swirlee at stickist dot com, gcc-help at gcc dot gnu dot org
- Date: Mon, 07 Mar 2005 18:06:45 +0000
- Subject: Re: Segfault with delete[] operator & virtually derived classes
- Organization: Codesourcery LLC
- References: <363801FFD7B74240A329CEC3F7FE4CC40215DD19@ntxboimbx07.micron.com>
lrtaylor@micron.com wrote:
you asked for an array of Bs, if you're not treating it as an array of
Bs it's not gonna work.
Why not? If B is derived from A, there should be nothing wrong with
storing pointers to B in an array of A pointers. Although, it seems
that A and B would need to have virtual destructors. I didn't read the
earlier part of the thread, so I don't know if that is the case here...
You are correct wrt an array of POINTERS to object (hence
my original suggestion to use just such an array). The question
was about an array of OBJECTS.
...you're lying to the compiler, and then kludging around the issue.
So, if B is derived from A and I do
A* a = new B;
am I lying to the compiler? That's what inheritance and polymorphism is
all about!
correct, but this is not what was being discussed.
the code was
A *ary = new B[10];
which is significantly different.
nathan
--
Nathan Sidwell :: http://www.codesourcery.com :: CodeSourcery LLC
nathan@codesourcery.com :: http://www.planetfall.pwp.blueyonder.co.uk