TITLE: returning multiple values (Source: comp.lang.c++, 27 Jan 2001) WAHLER: Mike Wahler (mkwahler@ix.netcom.com) >: In C++, a function can return only one value. KUEHL: "Dietmar Kuehl" ... which, of course, can be a composite of two values. WAHLER: >: #include >: >: void getFraction(int x, int y, int& result1, int&result2) >: { >: result1 = x * x; >: result2 = y * y; >: } KUEHL: ... or use something like this: std::pair getFraction(int x, int y) { return std::make_pair(x * x, y * y); } ... and with a little helper do this: template struct binder_t { binder_t(T1& v1, T2& v2): r1(v1), r2(v2) {} template void operator= (std::pair const& p) { r1 = p.first; r2 = p.second; } private: T1& r1; T2& r2; }; template binder_t binder(T1& r1, T2& r2) { return binder_t(r1, r2); } int main() { int r1; int r2; binder(r1, r2) = getFraction(2, 4); std::cout << "r1 = " << r1 << "\n"; std::cout << "r2 = " << r2 << "\n"; } _______________________________________________ cpptips mailing list http://cpptips.hyperformix.com