copy vs assign

Still a little fuzzy on this:
I know a new object can be initialized to an existing one using
myclass objectB(objectA);

But is it correct that an already existing object can NOT be (re)assigned to another already existing object using:
objectA(objectC);

instead of the normal assignment:
objectA = objectC;

Just checking.
closed account (S6k9GNh0)
It should be but I'm not sure. Try compiling it
Correct.

 
myclass B(A);


Calls myclass's copy constructor. Since a constructor can't be called on an object after it's already been constructed -- the only time you can do this is when you create the object.

Instead, B = A; calls the assignment operator which can be called any time after an object has been constructed.

The only tricky part is this:

 
myclass B = A;


This actually calls the copy ctor on B and not the assignment operator (even though it's using the assignment operator!).
Cool. thanks disch.
Topic archived. No new replies allowed.