Re: constructor and reset method
On Feb 27, 5:47 pm, "jason.cipri...@gmail.com"
<jason.cipri...@gmail.com> wrote:
"Alf P. Steinbach" <al...@start.no> wrote in message news:
13sa67q3k671...@corp.supernews.com...
Check out the swap idiom for implementing assignment
operator in terms of copy constructor.
Is that something like this (fromhttp://www.gotw.ca/gotw/059.htm):
T& T::operator=( const T& other )
{
T temp( other ); // do all the work off to the side
Swap( temp ); // then "commit" the work using
return *this; // nonthrowing operations only
}
That's the way I write it. Two other frequent alternatives are:
T&
T::operator=(
T const& other )
{
T( other ).swap( *this ) ;
return *this ;
}
and
T&
T::operator=(
T other )
{
swap( other ) ;
return *this ;
}
(Note that in the last case, the copy is actually done by the
caller, so you don't need it in the operator= function.)
And one advantage is there's really not much to do in the =
operator, since you are using the constructors to do a lot the
work?
The real advantage is that it is the simplest way to ensure that
all operations which can fail take place before you modify
anything in the object being assigned to.
So a Reset() might simply be:
void T::Reset () {
Swap(T());
}
But in that case... I mean, is there a way to implement Swap()
that doesn't involve explicitly typing out code to swap all of
the members?
Not really, since some of the members may have specialized swap
algorithms.
Since you just need to do a "shallow" swap... is something
like this reliable:
void T::Swap (T &other) {
char temp[sizeof(T)];
memcpy(temp, &other, sizeof(T));
memcpy(&other, *this, sizeof(T));
memcpy(*this, temp, sizeof(T));
}
No.
That seems like a horrible way to do it...
It is, since it involves undefined behavior.
Note that it's entirely possible that the swap idiom cannot be
used for certain classes---if there's no way to do a guaranteed
nothrow swap of some base class or member.
also, what if "other" is something derived from T?
What about it? Assignment cannot change the type of the object
being assigned, so you only assign the T part of the derived
object. (In general, assignment and polymorphism don't work
well together, and if a class is designed to be used as a base
class, it's usually best to inhibit assignment in it.)
Is there a way to just use the compilers default assignment
operator even when you've defined your own?
No. Presumably, if you provide your own assignment operator,
it's because the compiler's default assignment operator doesn't
do what you want.
Or does it always make sense to use the "pimpl" idiom when
implementing a swap function, so that you don't have to do
something like that?
In a certain sense, it always makes sense to use the swap idiom
for assignment if you use the compilation firewall idiom.
Except that in the case of the compilation firewall, you're
dealing with an easily recognizable idiom, and even the standard
implementation of operator= is so simple that I don't find it
worth the bother. Generally speaking:
-- if the class corresponds to an established idiom (like the
compilation firewall), then use the assignment operator for
the idiom, otherwise,
-- if all members support value nothrow value assignment, then
the compiler defined assignment operator is all you need; no
point in the swap idiom, otherwise,
-- if all of the members support value assignment, and you
don't need the strong exception guarantee, then the compiler
defined assignment operator is all you need as well,
otherwise,
-- if all of the members are either non-class types, or class
types which support a nothrow swap (generally, which have a
member function swap), then use the swap idiom, using
std::swap for the non-class types, and the nothrow swap for
the class types, otherwise
-- you're on your own, and it may be difficult.
In practice, if all of the libraries you're using understand the
swap idiom, and have been written in consequence, the fourth
case will be rather frequent. If you have to deal with legacy
libraries, however, you may find that you fall into the fifth
case more often than you'd like. In such cases, I find that it
often is convenient to use the compilation firewall idiom in
such cases, to move them up to the first case above.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orient=E9e objet/
Beratung in objektorientierter Datenverarbeitung
9 place S=E9mard, 78210 St.-Cyr-l'=C9cole, France, +33 (0)1 30 23 00 34