assignment operator question

This is a discussion on assignment operator question within the C++ Programming forums, part of the General Programming Boards category; Hello I have a class with copy assignment operator(s): Code: someclass instance1("construction"); someclass instance2 = instance1; Is there any way ...

  1. #1
    l2u
    l2u is offline
    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
    21,588
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,588
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 08:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21