Assignment operator?

Discussion in 'C++' started by Aneel, Apr 6, 2010.

  1. Aneel

    Aneel Guest

    Does assignment operator also creates object, as created by copy
    constructor, in classes. e.g.

    class check {
    int x;
    public:
    check(int a=0):x(a){cout<<"\nconstructor\n";}

    check operator = (const check &rhs){cout<<"\nassignment operator
    \n";}
    ~check(){cout<<"\ndestructor\n";}
    };

    int main() {
    check ob1(3), ob2(4);

    ob1=ob2; // does this create a temporary copy of ob1 or ob2;

    return 0;
    }


    return 0;
    }
    Aneel, Apr 6, 2010
    #1
    1. Advertising

  2. Aneel

    Jonathan Lee Guest

    On Apr 6, 10:36 am, Aneel <> wrote:
    > Does assignment operator also creates object, as created by copy
    > constructor, in classes. e.g.


    Not usually, but you've written operator=() to return by value.
    Normally you'd have a return value in that function and it would
    be

    return *this;

    With return by value, a copy will be made. To fix this, return
    by reference. i.e.,

    check& operator=(const check&) {
    // ...
    return *this;
    }

    --Jonathan
    Jonathan Lee, Apr 6, 2010
    #2
    1. Advertising

  3. Aneel

    Kai-Uwe Bux Guest

    Aneel wrote:

    > Does assignment operator also creates object, as created by copy
    > constructor, in classes. e.g.
    >
    > class check {
    > int x;
    > public:
    > check(int a=0):x(a){cout<<"\nconstructor\n";}
    >
    > check operator = (const check &rhs){cout<<"\nassignment operator
    > \n";}
    > ~check(){cout<<"\ndestructor\n";}
    > };
    >
    > int main() {
    > check ob1(3), ob2(4);
    >
    > ob1=ob2; // does this create a temporary copy of ob1 or ob2;
    >
    > return 0;
    > }
    >
    >
    > return 0;
    > }


    In the above case, it does not (*). But it could, e.g., in the copy-swap
    idiom:

    check & operator= ( check other ) {
    swap( *this, other );
    return ( *this );
    }

    (*) Well, according to the proposal for C++0X it does not. The current
    standard [8.5.3/5] allows that during initialization of a const& a temporary
    may be created. So calling the assignment operator could create a temporary,
    but (a) it need not, (b) it's unlikely to, and (c) should not in the
    (hopefully) near future.


    Best

    Kai-Uwe Bux
    Kai-Uwe Bux, Apr 6, 2010
    #3
  4. Aneel

    Guest

    On Apr 6, 10:36 am, Aneel <> wrote:
    > Does assignment operator also creates object, as created by copy
    > constructor, in classes. e.g.
    >
    > class check {
    >     int x;
    >   public:
    >     check(int a=0):x(a){cout<<"\nconstructor\n";}
    >
    >     check operator = (const check &rhs){cout<<"\nassignment operator
    > \n";}
    >     ~check(){cout<<"\ndestructor\n";}
    >
    > };
    >
    > int main() {
    >   check ob1(3), ob2(4);
    >
    >   ob1=ob2; // does this create a temporary copy of ob1 or ob2;
    >
    >   return 0;
    >
    > }
    >
    >   return 0;
    >
    > }
    >
    >


    In short, a copy constructor creates a new object in a new memory area
    and initializes it using the object copied from. An assignment
    operator "updates" an already existing object in memory using the
    object assigned from.

    HTH
    , Apr 6, 2010
    #4
  5. Aneel

    Noah Roberts Guest

    In article <3337f216-400c-4ac4-a60a-53f79886dbd2
    @e7g2000yqf.googlegroups.com>, says...
    >
    > Does assignment operator also creates object, as created by copy
    > constructor, in classes. e.g.
    >
    > class check {
    > int x;
    > public:
    > check(int a=0):x(a){cout<<"\nconstructor\n";}
    >
    > check operator = (const check &rhs){cout<<"\nassignment operator
    > \n";}
    > ~check(){cout<<"\ndestructor\n";}
    > };
    >
    > int main() {
    > check ob1(3), ob2(4);
    >
    > ob1=ob2; // does this create a temporary copy of ob1 or ob2;
    >
    > return 0;
    > }


    Impossible to know, really. Your assignment operator claims to return
    something but doesn't.

    --
    http://crazyeddiecpp.blogspot.com/
    Noah Roberts, Apr 7, 2010
    #5
    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. Wolfgang Groiss
    Replies:
    0
    Views:
    387
    Wolfgang Groiss
    Nov 19, 2003
  2. nagy
    Replies:
    36
    Views:
    974
    Terry Reedy
    Jul 20, 2006
  3. G Patel

    comma operator and assignment operator

    G Patel, Feb 7, 2005, in forum: C Programming
    Replies:
    4
    Views:
    475
    Barry Schwarz
    Feb 8, 2005
  4. Chris
    Replies:
    34
    Views:
    1,477
  5. Hicham Mouline
    Replies:
    2
    Views:
    677
    Juha Nieminen
    Sep 1, 2009
Loading...

Share This Page