• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: C++ STL question
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: C++ STL question


  • Subject: Re: C++ STL question
  • From: "Michael Crawford" <email@hidden>
  • Date: Tue, 11 Nov 2008 19:08:30 -0800

> I don't know if this is an appropriate question for this group, but
> here I go anyway..

The comp.lang.c++.moderated Usenet newsgroup would be the best place
to ask.  I expect there would be lots of Xcode users there.  You can
get it at Google Groups if you don't have a News server.

> I'm building a project with Leopard, Xcode 3, and GCC 4.0, and I
> noticed that I get the following warning when using the STL binder
> classes:
>
> warning: base class 'struct std::binary_function<myClass, myClass,
> bool>' has a non-virtual destructor

Well I have to ask - are you subclassing std::binary_function?

While it's perfectly *legal* to subclass a class or struct that has a
non-virtual destructor, unless you're very careful about how you use
it, it's also a *very* bad idea.

The reason is that if you call delete on an object of subclass type,
but through a pointer of base type, then *only* the base's destructor
will be called.  Your subclass' destructor won't be called at all!

Quite likely this will cause a memory leak, which may be worse than
you expect if the subclass has member variables that own other
objects.

At worst, it could cause some kind of erroneous behaviour.  I can even
see ways it could cause a crash.

In general, having a non-virtual destructor in C++ is effectively the
same as having the "final" keyword in Java: they both mean that
subclassing is not allowed.

It's just that in Java, "final" is enforced by the compiler, while in
C++, non-virtual destructors are enforced by subjecting the coder to a
great deal of grief, so that he eventually learns the error of his
ways.

I expect an argument can be made that C++'s "enforcement" is the
better implementation.

Mike
--
Michael David Crawford
mdcrawford at gmail dot com

   Enjoy my art, photography, music and writing at
http://www.geometricvisions.com/
        --- Free Compact Disc ---
 _______________________________________________
Do not post admin requests to the list. They will be ignored.
Xcode-users mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:

This email sent to email@hidden

References: 
 >C++ STL question (From: "Dan Caugherty" <email@hidden>)

  • Prev by Date: C++ STL question
  • Next by Date: XCode 3 auto-indent
  • Previous by thread: C++ STL question
  • Next by thread: Re: C++ STL question
  • Index(es):
    • Date
    • Thread