Thread: assignment operator

  1. #1
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391

    assignment operator

    are assignment operators only called in multiple assignment statements like:

    Code:
    obj3 = obj2 = obj1;
    or are there other times?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Assignment operators are called whenever you perform assignment.
    My best code is written with the delete key.

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    hmm... I am not entirely sure how to interpret your statement. I'm inclined to say yes, there are other times, such as when operator chaining is not used:
    Code:
    obj2 = obj1;
    But that sounds too obvious.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    but if I write something like this into the assignment operator:

    Code:
    if( &rhs != this ){
    
       std::cout << "Preforming assignment.\n";
    
       // normal assignment duties
    }
    
    return *this;
    I don't see the message for single assingment statements like:

    Code:
    obj2 = obj1;
    only in multiple assignments like:
    Code:
    obj3 = obj2 = obj1;
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Show more code, like a complete example.

    Are you sure you're not doing this:
    Code:
    Object obj1;
    Object obj2 = obj1;
    If that's the case, then you are calling the copy constructor for obj2, not the copy assignment operator. You would want to do this instead:
    Code:
    Object obj1;
    Object obj2;
    obj2 = obj1;

  6. #6
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by Daved View Post
    Show more code, like a complete example.

    Are you sure you're not doing this:
    Code:
    Object obj1;
    Object obj2 = obj1;
    If that's the case, then you are calling the copy constructor for obj2, not the copy assignment operator. You would want to do this instead:
    Code:
    Object obj1;
    Object obj2;
    obj2 = obj1;
    Yep, I was doing your first example. Why is that? Why is the copy constructor called instead of the assignment operator in that example?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Why is the copy constructor called instead of the assignment operator in that example?
    It happens to be copy constructor syntax.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by laserlight View Post
    It happens to be copy constructor syntax.
    because it's part of the declaration? Is that the only reason?
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  9. #9
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Code:
    Object obj1;
    Object obj2 = obj1;
    And
    Code:
    Object obj1;
    Object obj2(obj1);
    Are both copy constructor. When initializing an object, the copy constructor is called. This is regardless if you use () or =.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  10. #10
    Use this: dudeomanodude's Avatar
    Join Date
    Jan 2008
    Location
    Hampton, VA
    Posts
    391
    Quote Originally Posted by Elysia View Post
    Code:
    Object obj1;
    Object obj2 = obj1;
    And
    Code:
    Object obj1;
    Object obj2(obj1);
    Are both copy constructor. When initializing an object, the copy constructor is called. This is regardless if you use () or =.
    That's what I should have said, because it's part of the initialization. Thank you Elysia.
    Ubuntu Desktop
    GCC/G++
    Geany (for quick projects)
    Anjuta (for larger things)

  11. #11
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Why is the copy constructor called instead of the assignment operator in that example?
    Because you're constructing obj2.

    Built-in types are definied and initialized with that syntax, so the syntax was used for classes as well. For built-in types, there really isn't such a thing as construction, so assignment at initialization and assignment afterwards wasn't much different and shared the same syntax.

    Note that there are still some differences between the two syntaxes, although I forget what they are at the moment.

  12. #12
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Note that there are still some differences between the two syntaxes, although I forget what they are at the moment.
    If the copy constructor is declared explicit, only the:
    Code:
    Object obj2(obj1);
    form can be used.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  13. #13
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    I think the difference is when the right side is not an object of the same type.

    With the explicit constructor call syntax, the appropriate constructor is called.
    With the assignment initialization syntax, a temporary is created using the appropriate constructor, and then the actual object is initialized using the copy constructor.

    But I'm not 100&#37; sure about this. To test, make the copy constructor private and see if both forms work.

    Edit: laserlight probably knows better than me.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  14. #14
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    With the assignment initialization syntax, a temporary is created using the appropriate constructor, and then the actual object is initialized using the copy constructor.
    Yes, I think that is the case, but the compiler is allowed to elide the otherwise extra constructor call.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  15. #15
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    But only if it is side-effect-free, I think. Otherwise, no compiler would ever create the temporary. It's not like RVO or NRVO, where eliding the copy actually takes some implementation effort.
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Crazy Assignment Operator Semantics
    By SevenThunders in forum C++ Programming
    Replies: 7
    Last Post: 01-22-2009, 01:08 PM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM