Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > is it okay to call the overrided assignment function in copy constructor

Reply
Thread Tools

is it okay to call the overrided assignment function in copy constructor

 
 
jccpro@gmail.com
Guest
Posts: n/a
 
      08-15-2005
as the following code shows:

class A {
public:
A(const A & rhs) {
*this = rhs;
}

A & operator=(const A & rhs) {
if (this != &rhs) {
...
...
}
return *this;
}
};

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-15-2005
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> as the following code shows:
>
> class A {
> public:
> A(const A & rhs) {
> *this = rhs;
> }
>
> A & operator=(const A & rhs) {
> if (this != &rhs) {
> ...
> ...
> }
> return *this;
> }
> };
>


I see a couple of reasons why it might not be OK. First of all, the
object ('*this') hasn't been fully constructed until the constructor body
finishes executing and returns. That means that something can still be
unfinished about this object. The assignment operator, OTOH, assumes that
the object for which it's called is complete. That's a theoretical no-no.
Another, rather practical no-no, is if you try to use any virtual
function[s] in the assignment operator. Especially if you declare them
pure in class A.

V
 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-15-2005
(E-Mail Removed) wrote:
> as the following code shows:
>
> class A {
> public:
> A(const A & rhs) {
> *this = rhs;
> }
>
> A & operator=(const A & rhs) {
> if (this != &rhs) {
> ...
> ...
> }
> return *this;
> }
> };


The behaviour of the code above is well defined and probably what you
expect.
 
Reply With Quote
 
Earl Purple
Guest
Posts: n/a
 
      08-15-2005

(E-Mail Removed) wrote:
> as the following code shows:
>
> class A {
> public:
> A(const A & rhs) {
> *this = rhs;
> }
>
> A & operator=(const A & rhs) {
> if (this != &rhs) {
> ...
> ...
> }
> return *this;
> }
> };


There can be issues with doing it that way if operator=() calls new and
it fails either with bad_alloc or because the object you create throws
an exception

That is why the preferred way is to get operator= to call the
copy-constructor and swap. Thus:

class A
{
public:
A( const A & ); // well defined
void swap( A& ); // well defined, possibly private

A& operator=( const A& rhs )
{
A temp( rhs );
swap( temp );
return *this;
}
};

There is no need to check here for self-assignment either. While it
would be more efficient to check for it when you are self-assigning,
99.9% of the time you are not and on those occasions you are making a
saving by not checking for it.

 
Reply With Quote
 
jccpro@gmail.com
Guest
Posts: n/a
 
      08-15-2005
If there are some potential issues, I would rather declare an
initialize method to be called by both copy constructor and assignment
operator as below:

class A {
public:
A(const A & rhs) {
initialize(rhs);
}

A & operator=(const A & rhs) {
initialize (rhs);
return *this;
}

void initialize (const A & rhs) {
...
...
}
};

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
template copy constructor vs normal copy constructor cinsk C++ 35 10-10-2010 11:14 PM
A constructor calling another constructor (default constructor)? Generic Usenet Account C++ 10 11-28-2007 04:12 AM
Okay to override an ASP.NET page's constructor slolife ASP .Net 2 05-14-2007 09:39 PM
Does assignment operator have to behave exactly like copy constructor bluekite2000@gmail.com C++ 12 06-16-2005 09:44 PM
Re: ViewState + AutoIncrement -- Okay; Session + AutoIncrement -- Not okay Gene Gorokhovsky ASP .Net 0 07-17-2003 02:48 PM



Advertisments