Thread: how is it possible to return multiple values?

  1. #1
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497

    how is it possible to return multiple values?

    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)

    "...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.."
    Bill Bryson


  2. #2
    Registered User
    Join Date
    Dec 2008
    Location
    Black River
    Posts
    128
    Two ideas:

    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';
    }
    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)

  3. #3
    The superhaterodyne twomers's Avatar
    Join Date
    Dec 2005
    Location
    Ireland
    Posts
    2,273
    Or return a structure if you want to return a number of variable types.

  4. #4
    Registered User
    Join Date
    May 2009
    Posts
    16
    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.

  5. #5
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by vampirekid.13 View Post
    using pointers is probably your best bet.

    C++ Pointers

    theres a nice quicklink to a pointer tutorial if you need it.
    Yuk! I would use a std:: pair, boost::tuple or struct, or just pass reference parameters as stated above. I don't think pointers would be good in this case since there are easier and safer methods available.
    "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

  6. #6
    بابلی ریکا Masterx's Avatar
    Join Date
    Nov 2007
    Location
    Somewhere nearby,Who Cares?
    Posts
    497
    thank you all guys , i appreciate that . tanx
    Highlight Your Codes
    The Boost C++ Libraries (online Reference)

    "...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.."
    Bill Bryson


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  2. Smart pointer class
    By Elysia in forum C++ Programming
    Replies: 63
    Last Post: 11-03-2007, 07:05 AM
  3. need help program crashing
    By tunerfreak in forum C++ Programming
    Replies: 14
    Last Post: 05-22-2006, 11:29 AM
  4. OpenGL and Windows
    By sean345 in forum Game Programming
    Replies: 5
    Last Post: 06-24-2002, 10:14 PM
  5. Algorithm to walk through a maze.
    By Nutshell in forum C Programming
    Replies: 30
    Last Post: 01-21-2002, 01:54 AM