Re: interface programming

Tech Tip: Click here to run a free scan for Windows Errors and optimize PC performance



"Simon Trew" <noneofyour@xxxxxxxxxxxx> wrote in message
news:%23suYBt8VGHA.4772@xxxxxxxxxxxxxxxxxxxx
"dummy#1" <dummy1@xxxxxxxxxxxxxxxxxxxxxxxxx> wrote in message
news:0736F76C-D13F-4B88-892D-BE8F4A1CF983@xxxxxxxxxxxxxxxx
I am using *pure* virtual c++ class. I don't think I can put a
virtual dtor
in a pure virtual class. There is nothing in the class to
deallocate.

Yes you can-- in fact, it's almost always the right thing to do to
make the destructor virtual, and making it pure is no great shakes,
since the compiler will supply a default destructor if one is not
defined in the derived class.

Not if you declare pure virtual destructor. Perhaps surprisingly, one
can provide an implementation for pure virtual functions, and in case of
a destructor one must in fact do so. Like this:

class IMyClass {
public:
virtual ~IMyClass() = 0;
};

IMyClass::~IMyClass() {}

Note also that COM interfaces don't typically have destructors, virtual
or otherwise (beyond a trivial one provided by the compiler, of course).
A COM object is expected to delete itself from inside Release() call,
which means that 'delete' is always issued from the context of the most
derived class, so virtual destructor is unnecessary.
--
With best wishes,
Igor Tandetnik

With sufficient thrust, pigs fly just fine. However, this is not
necessarily a good idea. It is hard to be sure where they are going to
land, and it could be dangerous sitting under them as they fly
overhead. -- RFC 1925


.



Relevant Pages

  • Re: auto_ptr compile error
    ... delete statement from destructor of auto_ptr requires the declaration of destructor of wrapped object? ... it leaks sometimes. ... Owner is a class that owns an instance of Thing. ... If you take a closer look, there was actually a compiler warning about it, but no error. ...
    (microsoft.public.vc.language)
  • Re: Delete a class with multiple inheritance through base pointer?
    ... > the destructor of the derived class. ... Could you elaborate on that (probably with some sample code & which compiler ... Thus the dtor of the derived class is called before ...
    (comp.lang.cpp)
  • Re: Virtual dtor and placement new.
    ... templatevoid CallDestructor(T*p) instead of void * helps the ... > could then store a pointer to the original memory and call MyDelete ... The functor is a good solution, but then you don't call the destructor ... unwinded by the compiler. ...
    (comp.lang.cpp)
  • Re: why should i use a virtual deconstructor?
    ... Or the compiler might choose at random ... | destructor being called or a crash because of a non-virtual ... Thanks for the insights James. ...
    (alt.comp.lang.learn.c-cpp)
  • Re: refcounting and protecting against delete
    ... we are in the process of migrating to a component ... and I feel that since the compiler actually has this ... >I thought of making the destructor protected in IUnknown, ... delete to be friends of IUnknown? ...
    (comp.lang.cpp)