class initialization and the = operator

This is a discussion on class initialization and the = operator within the C++ Programming forums, part of the General Programming Boards category; I've already asked some people about this on AIM but I decided to just formaly ask it on the boards ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    40

    class initialization and the = operator

    I've already asked some people about this on AIM but I decided to just formaly ask it on the boards as well. I have several questions

    Whenever you initialize an object of a class, e.g

    Code:
    class_name ob1 = ob2; //ob2 previously declared
    does the '=' operator *always* decide what constructor to call? e.g-- in the example above, would the = operator examine the two operands and then instruct the compiler to call the copy constructor?

    and then for something like:

    Code:
    class_name ob1=5;
    I already asked some people, and they said that this statement will call the class_name(int) constructor.

    next question:

    but then, if you have:

    Code:
    class_name ob1;
    class_name ob2;
    ob1=ob2;
    the assignment operator here does not call any constructor, correct? the code that facilitates the actual copy is located within the operator function itself? if so, what is the point of (copy) constructors at all? couldn't the necessary code just be in the operator function?

    next:

    and then finally: i think i read somewhere that there is a fundamental difference between objects initialized to a user-defined value and objects just declared and then later assigned that same value is that true?, e.g

    Code:
    class_name ob1;
    class_name ob2;
    ob1=ob2;                            //is there something different here
    class_name ob3=ob1;         //than here?
    like one is a default initialization, then assignment, and the other is a non-default initialization?

    so then is there a difference in how the '=' operator acts in these expressions?


    phew... i think thats everything... thanks to anyone who has any input

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,793
    Quote Originally Posted by krygen
    Whenever you initialize an object of a class, e.g


    Code:
    class_name ob1 = ob2; //ob2 previously declared
    does the '=' operator *always* decide what constructor to call? e.g-- in the example above, would the = operator examine the two operands and then instruct the compiler to call the copy constructor?
    The assignment operator has nothing to do with the above code. It does not tell the compiler to call the copy constructor, rather the above code is effectively turned into the following by the compiler:

    Code:
    class_name ob1(ob2);
    Which of course calls the copy constructor. The compiler does this behind your back (sneaky bastard isn't it?).




    Quote Originally Posted by krygen
    but then, if you have:


    Code:
    class_name ob1;
    class_name ob2;
    ob1=ob2;
    the assignment operator here does not call any constructor, correct?
    This is different than the first code segment you had. This represents a call to the assignment operator and not a constructor. The two situations above are different. I think you are getting confused by the presence of the '=' in the first case; it looks like the assignment operator is involved in the first case but it is not.



    Quote Originally Posted by krygen
    Code:
    class_name ob1;
    class_name ob2;
    ob1=ob2;                            //is there something different here
    class_name ob3=ob1;         //than here?

    like one is a default initialization, then assignment, and the other is a non-default initialization?

    so then is there a difference in how the '=' operator acts in these expressions?
    The first two lines are default constructors, the third line is the assignment operator, and the last line is the copy constructor. It is not really a difference in how the =operator acts. The compiler is effectively changing what you wrote to something else behind the scenes and it appears to be confusing you (successfully I might add).
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Guest Sebastiani's Avatar
    Join Date
    Aug 2001
    Location
    Waterloo, Texas
    Posts
    5,643
    right. of course, you can see all of this yourself by printing to the screen for each case:

    Code:
    class object {
    public:
     object() {
      cout << "object();" << endl;
      }
     object(const object & rhs) {
      cout << "object(rhs);" << endl;
      }
     object & operator = (const object & rhs) {
      cout << "*this = rhs;" << endl;
      } 
     };
    
    int main(void) {
     object a, b = a;
     a = b;
     }
    [edit]
    too slow
    [/edit]
    Last edited by Sebastiani; 01-26-2005 at 05:32 AM.
    Code:
    if( numeric_limits< byte >::digits != bits_per_byte )
        error( "program requires bits_per_byte-bit bytes" );
    24bbs.cpp

  4. #4
    Registered User
    Join Date
    Jun 2004
    Posts
    40
    thanks guys, great explanations

Popular pages Recent additions subscribe to a feed

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