hello all, i wonder if it is possible in C++ , that a function returns 2 values (at least) together!? is it even possible ? if so who do we implement such a thing in a function?
Printable View
hello all, i wonder if it is possible in C++ , that a function returns 2 values (at least) together!? is it even possible ? if so who do we implement such a thing in a function?
Two ideas:
There are a lot of other ways: Pointers, passing / returning a vector that holds the results, etc. Of course, if the returning values are of different types, then you'll most likely have to use references, or a struct similar to pair (Like C++0x's tuple)Code:#include <utility>
#include <iostream>
std::pair<int, int> myfunc() {
return std::pair<int, int>(0, 1);
}
int myfunc(int& ret) {
ret = 1;
return 0;
}
int main()
{
std::pair<int, int> x = myfunc();
std::cout << x.first << ',' << x.second << '\n';
int tmp;
int y = myfunc(tmp);
std::cout << y << ',' << tmp << '\n';
}
Or return a structure if you want to return a number of variable types.
using pointers is probably your best bet.
http://www.codersource.net/c++_pointers.html
theres a nice quicklink to a pointer tutorial if you need it.
thank you all guys , i appreciate that . tanx