I have a class A, from which three classes Aa Ab and Ac are inherated.
In class A I have defined some functions by virtual foo()=0, which I implemented in each subclass. Each class is written in a seperated .h and .cpp file.
However, now I think it is possible to overload the operator+ INSIDE each class (including pure virtual in class A), such that something like
1 2 3 4 5
A *value = new Aa();
A value2 = *value + 1.0f;
This should be realizable, because the operator+ is part of the Aa class. Now, I would like to do something like
1 2 3 4 5
A *value = new Aa();
A value2 = 1.0f + *value;
This time, I expect I cannot overwrite the operator+, because it is not part of either class A or class Aa.
Define a non-member operator+ which forwards to the member operator+=, which is how it's normally done anyway.
A more important question: what do you expect your operator+ to return? If you return a value of type A (as you seem to do in your sample code), you're going to lose all the non-A members of Aa. If you plan to return a reference, then it should be operator+= anyway, to preserve semantics.
Interesting point. That might be a problem. Maybe I have to tell a few words about the overall plan. In my program, I would like to the capable of handling three data structures. The have some information in common (stored in class A) and in additon each structure was its own information (class Aa etc.). So, if i loose this information, this would be bad. My idea was to have one code for the complete program and use class A everywhere. Only when I start the programs main function, I decide wheater I work with class Aa or Ab etc., which might be depending on use input. So, I plan something like:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
A value = getValue();
A value2 = 1.0f + A // here use different functions, wheater A is Aa od Ab
value2.printf() // here use different functions, wheater A is Aa od Ab
Maybe my complete idea is corrupt. Then it was good to ask, but what is the solutation?
As long as you're dealing with objects (rather than references to them, or, possibly wrapped, pointers), you don't get runtime polymoprhism: your getValue() returns an A, never Aa or Ab. Your value and value2 are instances of A, not Aa or Ab. Your call to value2.printf() calls the member of A because that's value2's most derived type.
On a larger scale, "information in common" is not a reason to use inheritance, it's a has-a relationship, although your sample code shows common behavior (addition to float and member function print), which is more plausible. Give more details.
Those aren't references. Anyway, It only makes it more certain that implementing it with my = 2 + my; would be wrong: you don't clone a vehicle on a conveyor belt when you need to add two more wheels and junk the old one! You just bolt on those wheels to the vehicle you already have, with no copying. That's my += 2; (if you care to build a surrogate or some similar class - I would call it VehicleOnConveyor, since it isn't a Vehicle until you can .drive() it). Or just call a member function: my->attachAxle(); -- that's more clear, the reader won't have to try to guess two of what you're adding (wheels? seats? subwoofers?)
Thank you for your patience. Ok, lets say a car has subwoofers (which the bicicle doesnt have) and the bicicle has spokes (which the car doent have). Lets say all values have some kind of level, which can be upgraded.
So what i want to do is (something like)
newVehicle *a =&(2 + *oldvehicle);
So, I really want to copy, store it in a pointer to the base class vehicle (but keeping the bicicle or car class). The oldvehicle should NOT be upgraded. By the operator+, i want to upgrade all wheels (higher radius) and maybe more stable spokes or subwoofers, depneding on class.
I know thats just an example, and as every example, it is only a part of the problem. The real project is by far more complicated.