Re: C++ STL question
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