Bogdan Maier
Bogdan Maier

Reputation: 683

Classes - Get function - return more than one value

Let's suppose we have:

Class Foo{
    int x,y;

    int setFoo();
}

int Foo::setFoo(){
    return x,y;
}

All I want to achieve is form my get function to return more than one value. How can I do this?

Upvotes: 2

Views: 3908

Answers (7)

Abhishek Devloper
Abhishek Devloper

Reputation: 21

You cannot really return multiple values in c++. But you can modify multiple values by reference

Upvotes: 2

George
George

Reputation: 4674

You can not return more than 1 variable. But you can do pass by reference, and modify that variable(s).

// And you pass them by reference
// What you do in the function, the changes will be stored
// When the function return, your x and y will be updated with w/e you do.
void myFuncition(int &x, int &y)
{
    // Make changes to x and y.
    x = 30;
    y = 50;
}

// So make some variable, they can be anything (including class objects)
int x, y;
myFuncition(x, y);
// Now your x and y is 30, and 50 respectively when the function return
cout << x << " " << y << endl;

Edit: To answer your question on how to get: Instead of returning just 1 variable, you pass some variables, so your function can modify them, (and when they return), you will get them.

// My gen function, it will "return x, y and z. You use it by giving it 3 
// variable and you modify them, and you will "get" your values.
void myGetFunction(int &x, int &y, int &z)
{
    x = 20;
    y = 30;
    z = 40;
}

int a, b, c;
// You will "get" your 3 value at the same time when they return.
myGetFunction(a, b, c);

Upvotes: 1

Paul Manta
Paul Manta

Reputation: 31567

You can't return more than one object per se, but what you can do is use either std::pair from <utility> or std::tuple from <tuple> (the latter only available in the latest C++ standard) to pack more than one value together and return them as one object.

#include <utility>
#include <iostream>

class Foo
{
  public:
    std::pair<int, int> get() const {
        return std::make_pair(x, y);
    }

  private:
    int x, y;
};

int main()
{
    Foo foo;
    std::pair<int, int> values = foo.get();

    std::cout << "x = " << values.first << std::endl;
    std::cout << "y = " << values.second << std::endl;

    return 0;
}

Upvotes: 3

Griwes
Griwes

Reputation: 9031

You can use std::pair for two returned variables and std::tuple (C++11 only) for more of them.

Upvotes: 1

Luchian Grigore
Luchian Grigore

Reputation: 258548

C++ doesn't support multiple return values.

You can return via parameters or create an auxiliary structure:

class Foo{
    int x,y;

    void setFoo(int& retX, int& retY);
};

void Foo::setFoo(int& retX, int& retY){
    retX = x;
    retY = y;
}

or

struct MyPair
{
   int x;
   int y;
};

class Foo{
    int x,y;

    MyPair setFoo();
};

MyPair Foo::setFoo(){
    MyPair ret;
    ret.x = x;
    ret.y = y;
    return ret;
}

Also, shouldn't your method be called getFoo? Just sayin...

EDIT:

What you probably want:

class Foo{
    int x,y;
    int getX() { return x; }
    int getY() { return y; }
};

Upvotes: 10

Nicol Bolas
Nicol Bolas

Reputation: 473222

C++ does not allow you to return multiple values. You can return a type that contains multiple values. But you can only return one type from a C++ function.

For example:

struct Point { int x; int y; };

Class Foo{
    Point pt;

    Point setFoo();
};

Point Foo::setFoo(){
    return pt;
}

Upvotes: 1

Daniel A. White
Daniel A. White

Reputation: 190907

You can have reference parameters.

void Foo::setFoo(int &x, int &y){
    x = 1; y =27 ;
}

Upvotes: 6

Related Questions