It is a "good" practice for base class' destructor to be virtual. However, it
is not absolutely necessary and it won't always cause problem if you avoid the
special case. The following is the special case when your derived class
destructor would be skipped if your base class' destructor isn't virtual:
Class A
{
public:
A() { cout << "Called Class A Constructor\n" << flush;
~A() { cout << "Called Class A Destructor\n" << flush;
};
Class B : public A
{
public:
B() { cout << "Called Class B Constructor\n" << flush;
~B() { cout << "Called Class B Destructor\n" << flush;
};
void main()
{
cout << "Test 1: Class B destructor will be skipped\n" << flush;
A *b1 = new B;
delete b1;
cout << "Test 2: Everything is OK\n" << flush;
B b2;
}
On 23 Oct 2003 at 1:07, Riccardo Cohen wrote:
> Thanks a lot.
> First, of course, the virtual question was about an inherited class. I can't say
> why but even without the "virtual" keyword, my destructors are correctly called.
> lets say its ok ...
>
> I wrote a personnal version of new/delete inspired from palmoswerks.com :
>
>
> //////////////////////////////////////////
> void operator delete(void* ptr)
> {
> if (ptr)
> MemPtrFree(ptr);
> }
>
> //////////////////////////////////////////
> void* operator new(std::size_t size)
> {
> if (size == 0)
> size = 1;
> return MemPtrNew(size);
> }
>
>
> I wrote them in a .cpp and added a .h with :
>
> #include <new>
> void operator delete(void* ptr);
> void* operator new(std::size_t size);
>
> this .h is included everywhere through the settings C++ language / prefix file.
>
> It seams to work correctly on palm tungsten and palm 515.
> I am going wrong ?
>
>
> Thanks
>
> Ben Combee wrote:
>
> >
> >> I have learnt that every C++ class should have a virtual destructor,
> >> because of inheritance implementation, virtual ensure that the good
> >> destructor is called.
> >
> >
> > C++ classes should only have virtual destructors when they can be
> > inherited. There are plenty of uses of classes that don't support
> > inheritance. Do you need to inherit from a CString? Often, inheritance is
> > better replaced by encapsulation and aggregation anyway.
> >
> >> Moreover, it is written in doc "targeting_palm_os.pdf" that new and
> >> delete cant be called if !sysAppLaunchFlagNewGlobals because they
> >> throw exceptions and this needs globals.
> >
> >
> > That's right. You can use inline versions of new/delete (see
> > palmoswerks.com) to avoid this issue.
> >
> >> HOW CAN YOU PROGRAM IN C++ IN THOSE CONDITIONS ???
> >
> >
> > Very well. Just avoid the trouble spots, and use a mixture of OOP and
> > procedural programming to handle launches where globals aren't available.
> >
>
> --
> Riccardo Cohen
>
> Articque
> Les Roches
> 37230 Fondettes
> France
> web = http://www.articque.com
> tel: +33 02 47 49 90 49
> fax: +33 02 47 49 91 49
>
>
> --
> For information on using the Palm Developer Forums, or to unsubscribe, please
> see http://www.palmos.com/dev/support/forums/
>
>
John Leung
[EMAIL PROTECTED]
http://persweb.direct.ca/jleung
--
For information on using the Palm Developer Forums, or to unsubscribe, please see
http://www.palmos.com/dev/support/forums/