<fo*********@gmail.com> wrote in message
news:11**********************@g43g2000cwa.googlegr oups.com...
: Thanks All for the detailed explaination,
: do you mean I can not call a constructor explicitly?
:
: I've changed to use "this " to try to call constructor AA but failed
: (but i think destructor can be called explicitly),
:
: AA & operator=( AA & obj)
: {
: this->AA(obj);
: return *this;
: }
It would be possible to re-construct the object (by using
placement-new), but constructing an object twice will lead
to undefined behavior.
I have also seen programmers try first force the destruction
of the object ( this->~AA() ) before copy-constructing it,
but then in many cases it is impossible to provide basic
exception safety, and the legality of it remains uncertain.
If your class is simple enough for any of the bad tricks above
to work, you are better off in any case to implement the
copy-constructor itself in terms of the assignment operator:
AA( AA const& obj )
{
*this = obj;
}
for more complex objects, a common idiom is to use the
swap-with-temp-copy idiom, which is the solution Alf
pointed you to:
AA& operator=( AA const& obj )
{
AA(obj).swap(*this);
return *this;
}
Implementing an efficient swap operation is a good idea
anyway for most of the complex objects that are assignable.
hth-Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <>
http://www.brainbench.com