I'm not sure why you would want to return a reference, pointer, auto_ptr, or anything else besides a Color object. Is your Color class really that big? You are going to have to create a new object anyway (that's what the operator+ generally does), and you normally want to follow the same principles as the operator has on built-in types.
In his Exceptional C++ book, Herb Sutter has the following canonical form of operator+ (and operator +=) for some type T:
Code:
T& T::operator+=( const T& other )
{
// ... Do your adding here - add other's properties to this - jlou
return *this;
}
const T operator+( const T& a, const T& b )
{
T temp( a );
temp += b;
return temp;
}
Notice that you must have a working copy constructor for this to work. Some benefits include making sure that anytime you need to change how two Colors are added, you only have to change it in one place and both operator+= and operator+ are automatically updated and consistent. Also notice that elsewhere in your code, you should probably use += instead of + where you can if you are concerned with performance.
Also, in More Effective C++, Scott Meyers mentions that changing the operator+ to this:
Code:
const T operator+( const T& a, const T& b )
{
return T(a) += b;
}
might make it easier for the compiler to optimize away a temporary variable and avoid a call to the copy constructor.