Summary

 < Day Day Up > 



The goal of operator overloading is to give meaning to C++ operators in the context of user-defined data types. What an overloaded operator does to properly handle user-defined types is left to the discretion of the programmer. By overloading an operator for a particular class you answer the question 'What does this operation mean in the context of this class?'

Most operators belong to a family or group of related operators. Knowing how to overload one leads to an understanding of how to overload the others in the group.

Most operators can be declared either as class member functions or non-member functions. Some of the operators, like the assignment operator, must be declared as a class member function.

Non-member operator functions can be declared to be friend functions of the class on which they will operate. However, granting friendship status to such functions grants them access to a class's private attributes. This may or may not be in keeping with your class design goals.

The most often overloaded operator is the assignment operator. The purpose of the overloaded assignment operator is to properly handle deep copy operations. The compiler will provide a default assignment operator for your user- defined types but the default behavior is a bitwise member copy. Relying on the default assignment operator to properly handle complex class types usually results in disaster. Beware the default assignment operator and the effects of shallow vs. deep object copying.



 < Day Day Up > 



C++ for Artists. The Art, Philosophy, and Science of Object-Oriented Programming
C++ For Artists: The Art, Philosophy, And Science Of Object-Oriented Programming
ISBN: 1932504028
EAN: 2147483647
Year: 2003
Pages: 340
Authors: Rick Miller

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net