views:

556

answers:

7

I'm using the CPoint class from MFC. There is no explicitly defined assignment operator or copy constructor (AFAIK). Yet, this works:

CPoint p1(1, 2), p2;
p2 = p1; // p2 now is equal to p1

I'm assuming this is working automagically because of a compiler generated assignment operator. Correct?

If so, can I be confident that this isn't doing anything unexpected? In this case CPoint is so simple I think all is well, but in general this is something that worries me a bit. Is it better form to do:

p2.SetPoint(p1.x, p2.x);

-cr

+1  A: 

If a class is "simple", then the compiler-generated assignment operator will work (memberwise copy). If there are some members that would require more advanced logic (let's say the object maintains an internal pointer to what it expects is a private buffer), then the compiler-generated assignment operator will have issues. CPoint just stores an x and y coordinate of a point, so you should not run into issues.

Yuliy
I assume that for a widely used library, the developers would either provide an assignment operator if the default one doesn't work or declare a private assignment operator to prevent a public one from being emitted. In short, trust the default one.
criddell
A: 

I don't know MFC, but I can guess that (alternatively):

  • CPoint has a defined assignment operator (so not compiler-generated), or
  • Compiler-generated assignment operator works when members are stack-allocated, so it does memberwise copy.
tunnuz
Stack allocation has nothing to do with it; it would work just as well with heap-allocated CPoint objects. The important point is that the data in the class is plain data, with no ownership or uniqueness constraints.
David Thornley
I was speaking about stack-allocated members of the class, not objects of the class.
tunnuz
I obviously don't understand what you mean by "stack-allocated". Could you expand on that?
David Thornley
+4  A: 

This is safe - if an assignment operator wasn't meant to be supplied then the MFC designers could of made sure it wasn't available (by making it private for example).

IIRC the compiler will perform a member-by-member copy, so for a class containing POD like this, you won't have a problem. It can get messy if you have a class that allocates memory and neglects to override operator= and perform a deep-copy.

FWIW I asked a question about what the compiler can and cannot do a while back:

http://stackoverflow.com/questions/217911/why-dont-c-compilers-define-operator-and-operator

Some of the answers make for interesting reading.

Rob
A: 

The builtin copy assignment operator just copies each member in turn using their copy assignment operators. I assume that it's safe for CPoint without looking at its documentation (reason: If it wasn't they would provide an own implementation, of course). That point class should just have two members (x and y) and those are just floats (or int depending for what it's used).

One says it's a "shallow copy" since only the values of the members are copied. If you have pointer members, the pointer values are copied, instead of the objects where the pointers point to.

Johannes Schaub - litb
So the built-in one requires me to know implementation details of the class I'm using (but didn't write) and to trust that a future version of the class won't change significantly. Even though the builtin one is fine, I would still feel better if there were an explicit operator defined.
criddell
coryr, no it doesn't require you to know. It requires the author of that class to know, and he really should :) And you can trust that class to do the right thing. that's the whole point of classes
Johannes Schaub - litb
+3  A: 

Look up default copy constructor:

http://www.fredosaurus.com/notes-cpp/oop-condestructors/copyconstructors.html

This isn't a special thing about CPoint.

BobbyShaftoe
A: 

For a simple data object like CPoint (which you can see from the MFC source included with your Visual Studio installation is just a Win32 POINT structure with a few convenience functions tacked on), there is nothing wrong with using the compiler-generated assignment operator.

But as already mentioned, the default assignment operator is a shallow copy and will get you into trouble if the structure contains pointers (or contains structures that contain pointers without defining an assignment operator). Since CPoint doesn't fit that description, it's safe.

Joel
A: 

Yes. If you define no operator= method on a class, the compiler generates one for you that simply does a bitwise copy of the fields in the class. As I recall, CPoint is merely {int x; int y}, and so bitwise copy is fine.

Charlie Martin