Tuesday, September 9, 2008

Apple Certification Exam 9L0-509

The assignment operator, =, must be a member function, and is given default behavior for user-defined 9L0-509 classes by the compiler, performing an assignment of every member using its assignment operator. This behavior is generally acceptable for simple classes which only contain variables. However, where a class contains references or pointers to outside resources, 9L0-402 Exam the assignment operator should be overloaded (as general rule, whenever a destructor and copy constructor are needed so is the assignment operator), otherwise, for example, two strings would share the same buffer and changing one would change the other.

In this case, an assignment operator should perform two duties:

1. clean up the old contents of the object
2. copy 9L0-509 Braindump the resources of the other object

For classes which contain raw pointers, before doing the assignment, the assignment operator should check for self-assignment, which generally will not work (as when the old contents of the object are erased, they cannot be copied to refill the object). Self assignment is generally a sign of a coding error, and thus for classes without raw pointers, 9L0-402 this check is often omitted, as while the action is wasteful of cpu cycles, it has no other effect on the code.

No comments: