By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
429,251 Members | 2,760 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 429,251 IT Pros & Developers. It's quick & easy.

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

P: n/a
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?

Aug 3 '07 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Weihui Shen wrote:
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.
Aug 3 '07 #2

P: n/a

Weihui Shen <sw****@gmail.comwrote in message...
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

--
Bob R
POVrookie
Aug 3 '07 #3

P: n/a
On 8 3 , 2 40 , "BobR" <removeBadB...@worldnet.att.netwrote:
Weihui Shen <swl...@gmail.comwrote in message...
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

--
Bob R
POVrookie
Sorry for my obscure question.
Please consider the following code:

class A {
public:
A(int a) {
*this = a; // call A::operator=(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.

Aug 3 '07 #4

P: n/a
class A {
public:
A(int a) {
*this = a; // call A::operator=(int), it's safe?
May be I should write this ctor like this:

A(int x) {
*this = x;
}

Aug 3 '07 #5

P: n/a
On Aug 3, 9:16 am, Weihui Shen <swl...@gmail.comwrote:
On 8 3 , 2 40 , "BobR" <removeBadB...@worldnet.att.netwrote:
Weihui Shen <swl...@gmail.comwrote in message...
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?
[...]
Please consider the following code:
class A {
public:
A(int a) {
*this = a; // call A::operator=(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::operator=(
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.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Aug 3 '07 #6

This discussion thread is closed

Replies have been disabled for this discussion.