Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Is it a good idea to implement constructors with assignment operator?

Reply
Thread Tools

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

 
 
Weihui Shen
Guest
Posts: n/a
 
      08-03-2007
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?

 
Reply With Quote
 
 
 
 
Gianni Mariani
Guest
Posts: n/a
 
      08-03-2007
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.
 
Reply With Quote
 
 
 
 
BobR
Guest
Posts: n/a
 
      08-03-2007

Weihui Shen <> wrote 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


 
Reply With Quote
 
Weihui Shen
Guest
Posts: n/a
 
      08-03-2007
On 8 3 , 2 40 , "BobR" <removeBadB...@worldnet.att.net> wrote:
> Weihui Shen <swl...@gmail.com> wrote 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: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.

 
Reply With Quote
 
Weihui Shen
Guest
Posts: n/a
 
      08-03-2007
> class A {
> public:
> A(int a) {
> *this = a; // call A:perator=(int), it's safe?


May be I should write this ctor like this:

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

 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      08-03-2007
On Aug 3, 9:16 am, Weihui Shen <swl...@gmail.com> wrote:
> On 8 3 , 2 40 , "BobR" <removeBadB...@worldnet.att.net> wrote:
> > Weihui Shen <swl...@gmail.com> wrote 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: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: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.

--
James Kanze (GABI Software) email:
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


 
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
Is the possible to have all the public constructors of the publicbase class as the constructors of a derived class? Peng Yu C++ 5 09-19-2008 10:19 AM
compiler synthesized constructors/copy constructors/assignment operators Jess C++ 5 06-07-2007 11:09 AM
Copy constructors, de/constructors and reference counts Jeremy Smith C++ 2 08-02-2006 11:25 PM
Nested conditional expressions ---- good idea/bad idea? nimmi_srivastav@yahoo.com C Programming 10 02-02-2005 10:51 PM
Constructors that call other Constructors Dave Rudolf C++ 12 02-06-2004 03:26 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57