Classes - Get function - return more than one value

c++, class, return-value

Solution

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

Problem

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?

Original source