Is it a good idea to implement constructors with assignment operator?

W

Weihui Shen

Hello. Sometimes I see that some class constructors were implemented
with assignment operator and I wanna know whether it is safe to do
that or not?
 
G

Gianni Mariani

Weihui said:
Hello. Sometimes I see that some class constructors were implemented
with assignment operator and I wanna know whether it is safe to do
that or not?

An example of code you're referring to would really help.

My crystal ball tells me no.
 
B

BobR

Weihui Shen said:
Hello. Sometimes I see that some class constructors were implemented
with assignment operator and I wanna know whether it is safe to do
that or not?

Are you asking about 'initialization lists'?

class Sex{
int x, y;
public:
Sex() : x( 0 ), y( 0 ){}
Sex( int u, int v ) : x( u ), y( v ){}
};
Not only safe, it's the best way to do it (unless you have good cause not
to).

Or, did you mean this:

class Sex{
int x, y;
public:
Sex(){ x= 0 ; y= 0; }
Sex( int u, int v ){ x= u; y= v; }
};
If you need to. It's safe.

Or, did you mean this:

class Sex2{ public:
int x, y;
Sex2& operator=( Sex2 const &sx){
x = sx.x;
y = sx.y;
return *this;
}
};
It's safe.

Otherwise, show what you mean.

FAQ http://www.parashift.com/c++-faq-lite
 
W

Weihui Shen

Are you asking about 'initialization lists'?

class Sex{
int x, y;
public:
Sex() : x( 0 ), y( 0 ){}
Sex( int u, int v ) : x( u ), y( v ){}
};
Not only safe, it's the best way to do it (unless you have good cause not
to).

Or, did you mean this:

class Sex{
int x, y;
public:
Sex(){ x= 0 ; y= 0; }
Sex( int u, int v ){ x= u; y= v; }
};
If you need to. It's safe.

Or, did you mean this:

class Sex2{ public:
int x, y;
Sex2& operator=( Sex2 const &sx){
x = sx.x;
y = sx.y;
return *this;
}
};
It's safe.

Otherwise, show what you mean.

FAQ http://www.parashift.com/c++-faq-lite

Sorry for my obscure question.
Please consider the following code:

class A {
public:
A(int a) {
*this = a; // call A::eek:perator=(int), it's safe?
}
A& operator =(int b) {
a = b;
return *this;
}
private:
int a;
};

I mean that calling A's assignment operator in A's constructor.
 
J

James Kanze

[...]
Please consider the following code:
class A {
public:
A(int a) {
*this = a; // call A::eek:perator=(int), it's safe?
}
A& operator =(int b) {
a = b;
return *this;
}
private:
int a;
};
I mean that calling A's assignment operator in A's constructor.

As a general rule, no. In general, an assignment operator can
only be called on a fully constructed object.

The usual idiom, in fact, is the reverse: use the copy
constructor to implement assignment. This is often associated
with a shallow swap, to give something like:

A&
A::eek:perator=(
A const& other )
{
A tmp( other ) ;
tmp.swap( *this ) ; // but ONLY if swap is shallow!!!
return *this ;
}

The idea, of course, is that any operations that might fail will
have taken place before any modification to the object.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top