Thread: Overloading operator

  1. #1
    Unregistered
    Guest

    Overloading operator

    I need to be able to add to same structures. For example

    Code:
    CTest test1;
    CTest test2;
    
    test1 + test2;
    How can I overload the + operator to do something like that?

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    > test1 + test2;

    First off, this is totally invalid code. Consider:

    a + b; //what is this supposed to do?

    Do you mean:

    test3 = test1 + test2;

    Or perhaps:

    test1 += test2;


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Unregistered
    Guest
    Come on, i thought you could understand. Of course I meant Test3 = test1 + test2;

  4. #4
    Unregistered
    Guest
    Anybody??

  5. #5
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Unregistered
    Come on, i thought you could understand. Of course I meant Test3 = test1 + test2;
    So I'm supposed to assume that you meant "a = b + c" when all you list is "b + c", and this means that you really didn't mean "b = b + c"? Right? Why exactly? Because I'm a mind reader?

    Do you know how to overload operators? Any C++ book should cover it. Basicly all you do is do the overloading in a nice little function that adds all the values of the two classes.

    Keep in mind that you'll have to do all of the work correctly yourself when you write the function:

    Code:
    class Foo {
        int a;
        float b;
        String c;
    };
    You'll have to make sure you add all of the values the way you want. Unlike me, your compiler is not a mind reader so you must EXACTLY specify the way you want each value added.

    Get used to being specific now, or don't bother programming.

    I suppose you want everything handed to you on a silver platter, but that's not how things are typically done in the Real World(TM).

    Quzah.
    Last edited by quzah; 06-25-2002 at 05:18 PM.
    Hope is the first step on the road to disappointment.

  6. #6
    Unregistered
    Guest
    Ok, Im sorry, its very late here. Naw, I dont want everything given to me on a silver plate

    However, thank you for the tutorial.

  7. #7
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Posts
    2,138
    Depends on your class. What do you want added? What do you want done with other data? Give more data, and I will gladly help you.

  8. #8
    Code Monkey Davros's Avatar
    Join Date
    Jun 2002
    Posts
    812
    Don't worry, I know what you meant. You do it like this:

    Code:
    class Foo
    {
      Foo();
      Foo(const Foo& rhs);
      ..
      ..
      Foo& operator =(const Foo& rhs);
      Foo operator +(const Foo& rhs);
      
    };

    You can add all the operators you need, including ==, +=, -=, != etc.

    Quzah is correct in that you must take care to ensure that these operators do exactly what you intend. For example, consider a class that has some data held in the heap, i.e. it holds a private pointer to that data.

    If you were simply to copy the instance memory block in an assign (=) operation, you copied instance would simply hold a pointer to the same heap memory as the source instance. This typically were operators are important.

    For example, consider an object which has void* pData, and an int pDataSize as private. I.e.

    Code:
    class Foo
    private:
      void* pData;
      int pData;
      ...
      ...
    An assignment operator would be implemented as follows:

    Code:
    Foo& Foo:operator =(const Foo& rhs)
    {
      // Delete any current data
      delete[] pData;
    
      // Re-create data of new size
      pData = new char[rhs.pDataSize];
      memcpy(pData,  rhs.pData, rhs.pDataSize);
      pDataSize = rhs.pDataSize;
      return *this;
    }
    Note the use of const references in the parameters is an optimisation.

    I would suggest looking at method listing for the STL string class to get an idea of how to form common operators.

    Davros

    Edit : Finally got code tags to work!
    Last edited by Davros; 06-25-2002 at 05:39 PM.

  9. #9
    Registered User
    Join Date
    Jun 2002
    Posts
    44

    Talking Overloading operators

    Quzah sounds like a compiler .

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. unary operator overloading and classes
    By coletek in forum C++ Programming
    Replies: 9
    Last Post: 01-10-2009, 02:14 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. Operator Overloading (Bug, or error in code?)
    By QuietWhistler in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2006, 08:38 AM
  4. C++ Operator Overloading help
    By Bartosz in forum C++ Programming
    Replies: 2
    Last Post: 08-17-2005, 12:55 PM
  5. operator overloading and dynamic memory program
    By jlmac2001 in forum C++ Programming
    Replies: 3
    Last Post: 04-06-2003, 11:51 PM