I need to be able to add to same structures. For example
How can I overload the + operator to do something like that?Code:CTest test1; CTest test2; test1 + test2;
I need to be able to add to same structures. For example
How can I overload the + operator to do something like that?Code:CTest test1; CTest test2; test1 + test2;
> 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.
Come on, i thought you could understand. Of course I meant Test3 = test1 + test2;
Anybody??
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?Originally posted by Unregistered
Come on, i thought you could understand. Of course I meant Test3 = test1 + test2;
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:
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.Code:class Foo { int a; float b; String c; };
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.
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.
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.
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.
An assignment operator would be implemented as follows:Code:class Foo private: void* pData; int pData; ... ...
Note the use of const references in the parameters is an optimisation.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; }
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.
Quzah sounds like a compiler .