Thread: assignment operator question

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    630

    assignment operator question

    Hello

    I have a class with copy assignment operator(s):
    Code:
    someclass instance1("construction");
    someclass instance2 = instance1;
    Is there any way to write this code in one line?

  2. #2
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    well if you want to copy something, it takes at least one statement. also to copy something, you have to have it first, which also takes the one statement prior.

    you could also have a constructor that takes in a 'someclass', but that basically does the same thing as what you have here. i cant think of a way to reduce it to one statement.
    heres an example, note you still need to have the statement prior that declares instance1
    Code:
    someclass instance2 = someclass(instance1);

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    I have a class with copy assignment operator(s):
    You only use two constructors in your example code: one to construct a someclass object given a string, the other is the copy constructor. The copy assignment operator is not used.

    Is there any way to write this code in one line?
    Well, unless there are some unusual semantics you could write:
    Code:
    someclass instance1("construction"), instance2("construction");
    I would rather have it on two lines though.
    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
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    Quote Originally Posted by laserlight View Post
    You only use two constructors in your example code: one to construct a someclass object given a string, the other is the copy constructor. The copy assignment operator is not used.
    isnt the '=' the copy operator in action? or is it simply a reference assignment, as per normal '=' usage because instance2 is null.
    i thought it was the copy operator being used, so if you could clarify that would also help me!

  5. #5
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    When you are initializing (rather than assigning already assigned variables), the basic constructor can be used, so "X x = X()" doesn't need the assignment operator, just a basic constructor call.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    isnt the '=' the copy operator in action?
    In this context, no.
    Code:
    someclass instance2 = instance1;
    is equivalent to:
    Code:
    someclass instance2(instance1);
    except that the former is not allowed if the copy constructor is declared explicit.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM