Re: destruction of already destructed pointer variable when copying
an object - abort error
On Sep 11, 9:48 am, Juha Nieminen <nos...@thanks.invalid> wrote:
Goran <goran.pu...@gmail.com> wrote:
1. understand "the rule of three" (http://en.wikipedia.org/wiki/
Rule_of_three_%28C%2B%2B_programming%29)
Minor nitpick, but I think it's a bit incorrect to say that
"if a class defines one of the following it should probably
explicitly *define* all three" (emphasis mine).
In many cases it's sufficient to *declare* (rather than
define) the copy constructor and assignment operator (and
declare them private), if your class is such that it requires
a user-defined destructor.
Yes. The rule of three only applies to copiable classes; when
it was postulated, I don't think it was widely realized that a
lot of classes shouldn't be copiable. And of course, if you
don't want the class to be copiable, you don't even have to
declare the copy constructor and the assignment operator---just
derive from boost::noncopyable.
(BTW: I sort of miscounted in my earlier postings, speaking of
the rule of four. Although there really are four requirements,
since you definitely need at least one additional constructor,
other than the copy constructor.)
Often this is a much easier and less laborious way of adding
the safety measure without having to actually go through the
trouble of fully implementing copying (especially since in
many cases there's no clear "best" approach at copying an
object; should it use deep-copying, lazy copying, or just
reference counting, or perhaps something else completely?)
That's generally true. In his case, however, he's defining a
comparator functional object, and if it isn't copiable, he's not
going to be able to use it with the standard library (and
particularly, with min_element, which is what he wants).
2. class FO : public boost::noncopyable { ...
Is there any other advantage of boost:noncopyable other than
that it saves you from writing two lines of code in the
private section of the class?
It's more readable. And it appears where you'd expect such
information to appear.
Many, many years ago (before Boost), I used a class
UncopiableObject, for the same thing. I dropped it, because it
was unknown---while it looks more readable if you know what's
going on, it wasn't standard, and readers other than myself had
to find out what it meant (despite the name). Boost has become
more or less a pseudo-standard, however, at least for this, and
I would expect any reasonably experienced C++ programmer to
recognize the class, and what it does.
(And no, "it gives a better error message" is not a good
enough argument for requiring Boost to be installed in the
system.
Obviously, I wouldn't install Boost *just* to get this class.
There are other things in Boost that are useful, however, and
given a choice, I'd rather have Boost installed than not have
it.
Anybody who is even half-competent at C++ will understand what
it means if the copy constructor or assignment operator of a
class is inaccessible because it has been declared private.)
Yes, but they have to go looking for it, rather than having it
spelled out clearly at the top of the class.
--
James Kanze