This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Unexpected C++ program result
- From: Jonathan Wakely <jwakely dot gcc at gmail dot com>
- To: klperez <klperez97 at yahoo dot com>
- Cc: gcc-help at gcc dot gnu dot org
- Date: Wed, 9 May 2012 23:43:40 +0100
- Subject: Re: Unexpected C++ program result
- References: <CAHbtX-t1Q+N5it3DpYtzPEfW9C45MeVJ5qv9Rbhjw3Bk_W2fZg@mail.gmail.com> <33763652.post@talk.nabble.com>
On 9 May 2012 20:10, klperez wrote:
>
> Hello Mattia, I am glad your question was sort of answered. Given his answer
> it seems to me that gcc breaks the rules of polymorphism, because it does
> not allow multiple inherited objects to be treated like their base class.
No, GCC doesn't break anything.
A Final "is a " Base.
But an array of FInal is not an array of Base.
> Clearly one should not have Final objects be stored in a dynamically
> allocated array of base classes, but is there a way to store them in a
> different way without leaking memory?
Yes, of course:
Final* p = new Final[2];
delete[] p;
> For example could one use a std
> vector, which stores three Final objects, and then calls clear when it comes
> time to delete them?
Yes, of course.