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?
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?
Highlight Your Codes
The Boost C++ Libraries (online Reference)
Bill Bryson"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."
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.
"I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008
"the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010
thank you all guys , i appreciate that . tanx
Highlight Your Codes
The Boost C++ Libraries (online Reference)
Bill Bryson"...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.."