flexible class operators

Hey People,

I have a very simple class State containing a bunch of variables. Now I want to define State State::operator + (State) that simply returns a State object of which the values of the variables are the sum of the same variables, eg:

State State::operator + (State b)
  State result;
  result.x = x + b.x;
  result.vel = vel + b.vel;
  return result;

. Now I want this code to be ajustable for other problems, so that ideally I just have to add a variable in the class definition and the operator will automaticly include it in its operation. (Not just because I'm lazy, but also because I want others to be able to use the code without problems, so it should be as straight forward as possible). Can this be done?
Last edited on
Now I want this code to be ajustable for other problems

Assuming your defintion of operator + continues to mean that each of the variables are added, then yes. e.g If you add a variable y to your class, then certainly you can add result.y = y + b.y; to your operator function.
Thank you, but that's not what I'm looking for. The point is I want to operator to include all variables without me explicitly telling it to do so (ie. an operator that goes through all variables without naming them explicitly).
Nope. You're not going to be able to do that.

You could use an array or vector to hold the variables, then it would be a simple matter to iterate through the array or vector performing the addition on each member.

Topic archived. No new replies allowed.