Passing object objects by reference is fine, but you should know that many C ++ algorithms copy function objects, so if you need a library algorithm to respect your state, you must pass it as a link inside your function object:
struct State { double a, b, x, y; }; struct Function { State &state; explicit Function(State &state): state(state) {} double operator() (int v, int w) {....} }; State state; std::...(..., Function(state), ...)
In addition, some library algorithms (e.g. transform ) require pre-C ++ 11 that the function object has no side effects, i.e. no state at all; this requirement is rarely applied and mitigated in C ++ 11.
source share