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 ...

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

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?).

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.

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