+ Operator

This is a discussion on + Operator within the C++ Programming forums, part of the General Programming Boards category; I have a question, im making + operator for one of my classes, the code is this: Code: const Color ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    3

    Question + Operator

    I have a question, im making + operator for one of my classes, the code is this:
    Code:
    const Color & operator + ( const Color & a, const Color & b );
    The question is: i have to make a new object so i can return it, however if i make an auto object it will be deleted after the function ends ( right ? ) and if i make it with
    Code:
    Color * color = new Color;
    It wont be deleted after assignement... So how would i do it? i dont really want to pass the Color class entirely since it isnt small enough.


    thanks

  2. #2
    Super Moderator
    Join Date
    Sep 2001
    Posts
    4,913
    i dont really want to pass the Color class entirely since it isnt small enough.
    I think this would be a perfect application for pointers, don't you?

  3. #3
    vae victus! skorman00's Avatar
    Join Date
    Nov 2003
    Posts
    594
    you could return a Color object instead of a const Color &. If it is really that big though, you may want to make an Add function that takes 2 inputs and the output as parameters.

  4. #4
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    you could return an auto_ptr instead of a regular pointer. That way when the auto_ptr goes out of scope the item pointed to will be freed

  5. #5
    Registered User jlou's Avatar
    Join Date
    Jul 2003
    Posts
    1,088
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Failure to overload operator delete
    By Elysia in forum C++ Programming
    Replies: 16
    Last Post: 07-10-2008, 02:23 PM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 08:05 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 03:39 PM
  4. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-07-2003, 12:51 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21