Re: Can't compile this code *****SOLVED****

From: Chris Val (chrisval_at_bigpond.com.au)
Date: 11/26/04


Date: 25 Nov 2004 19:25:51 -0800

Karl Heinz Buchegger <kbuchegg@gascad.at> wrote in message news:<41A608DB.4AD3D766@gascad.at>...
> Morten Gulbrandsen wrote:
> >
> > > On the other hand, it might be that this is indeed an exercise
> > > in pointer syntax and using pointers which eventually will lead
> > > to polymorphism. One of the standard exercises in polymorphism
> > > is to have a Cat, a Dog and a virtual MakeNoise function.
> > >
> >
> > This I don't understand, very interesting,
> > I am willing to take a close look at this.
>
> OK. A short introduction

[snipped and enjoyable read :-)]
 
> class Parrot : public Pet
> {
> public:
> virtual void MakeNoise() { cout << "I want a cookie\n"; }
> };

[snip]

Hehe - In Australia, our Parrot's hypothetically eat 'Crackers' :-)
 
> Now, your real program might contain hundreds of code sequences
> like the above, and you would be forced to update all of them
> or your program wouldn't work for the 'Parrot-case' correctly.
> Compared with the 'just add a class and implement the virtual
> function' this is much more trouble.

Just one important factor left to mention :-)

It is mandatory, that the base class implements a 'virtual'
destructor, so that when de-allocating objects created via
a base pointer and the new operator, the destructors for
each of the derived objects will be called as appropriate.

Cheers.
Chris Val



Relevant Pages

  • Re: Cant compile this code *****SOLVED****
    ... > It is mandatory, that the base class implements a 'virtual' ... > destructor, so that when de-allocating objects created via ...
    (alt.comp.lang.learn.c-cpp)
  • Re: Virtual destructors are unique virtuals right
    ... The destructor of a derived class does not call the ... >> of its base class, regardless of whether the base class has a virtual ... constructor for the BaseClass, then calls the constructor for the ... DerivedClass. ...
    (comp.lang.cpp)
  • Re: Odd behavior, vector member, MFC and consol app
    ... someone call the base class destructor which then ended up doing something bad. ... it should have no virtual functions at all. ... A good clue that a class is not intended for derivation is the absence ...
    (microsoft.public.vc.mfc)
  • Re: inhibit compiler warning C4624 for a class hierarchy
    ... Any class must have a destructor before you can ... create (or, to be exact, destroy) an instance of it. ... implicitly-declared destructor, ... a base class with an inaccessible destructor. ...
    (microsoft.public.vc.language)
  • Re: My final shape program, what do you think?
    ... >> pointer to its base class then the base class must have a virtual ... >> destructor or its undefined behaviour. ... > i should have to use a virtual deconstructor if i want dervive ...
    (comp.lang.cpp)

Loading