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

Discussion in 'C++' started by Weihui Shen, Aug 3, 2007.

  1. Weihui Shen

    Weihui Shen Guest

    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?
     
    Weihui Shen, Aug 3, 2007
    #1
    1. Advertising

  2. 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.
     
    Gianni Mariani, Aug 3, 2007
    #2
    1. Advertising

  3. Weihui Shen

    BobR Guest

    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
     
    BobR, Aug 3, 2007
    #3
  4. Weihui Shen

    Weihui Shen Guest

    On 8 3 , 2 40 , "BobR" <> wrote:
    > 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


    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.
     
    Weihui Shen, Aug 3, 2007
    #4
  5. Weihui Shen

    Weihui Shen Guest

    > class A {
    > public:
    > A(int a) {
    > *this = a; // call A::eek:perator=(int), it's safe?


    May be I should write this ctor like this:

    A(int x) {
    *this = x;
    }
     
    Weihui Shen, Aug 3, 2007
    #5
  6. Weihui Shen

    James Kanze Guest

    On Aug 3, 9:16 am, Weihui Shen <> wrote:
    > On 8 3 , 2 40 , "BobR" <> wrote:
    > > 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?


    [...]
    > 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.

    --
    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
     
    James Kanze, Aug 3, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Russ Ford
    Replies:
    1
    Views:
    5,322
    Ron Natalie
    Nov 20, 2003
  2. Replies:
    10
    Views:
    1,262
    Big K
    Feb 2, 2005
  3. John Ratliff
    Replies:
    2
    Views:
    362
    John Ratliff
    Aug 10, 2005
  4. Chris
    Replies:
    34
    Views:
    1,565
  5. Jess
    Replies:
    5
    Views:
    626
    Ron Natalie
    Jun 7, 2007
Loading...

Share This Page