Can you return multiple named values from a function?

So I'm a weird mix of new to C++ but been programming different(easier) languages for a while, so bare with me please.
I'm trying to make a program to draw in 3d, I created a class for a cube 3d model(as a test) and gave it data for verticies and polygons and gave it an origin xyz point and created an object from that, then did the same with a camera. So here's my thought, I want to be able to switch cameras as needed or have multiple cameras on stage for multiple players, so the projected values of these models will be different depending on what camera is viewing it(projected values being how the xyz coordinate transfers to the xy point on the screen)

so in short:
I need a function kind of like how SFML has
shape.getPosition().x
only in my case it's
3dModelA.model_getProjected(camera1,3).x
3dModelA.model_getProjected(camera1,3).y
3dModelA.model_getProjected(camera1,3).lightLevel
3dModelA.model_getProjected(camera1,3).etc

where 3dmodel is an object created from the 3dmodel class, and camera is also an object created from the camera class, and in this case it's using the values from 3dModelA and camera1 to calculate the projected x, y, z, lightLevel, among other things from verticiy #3

I realize I could just have a function that returns an 2d array and ask for the specific value of the array that coincides with the number I'm looking for, but

3dModelA.model_getProjected(camera1,3).x

is a lot more comprehensive than:

a = model_getProjected(3dModelA,camera1)
cout<<a[1][2]<<endl;
yes.

you can use references as one way:
void foo(thing &output1, otherthing & output2, something input) … like that and fill in the output variables.

you can also wrap the outputs into some sort of container and return that:

struct retval
{
thing one;
other thing two;
};

retval foo(something input)

you don't have to use a struct. you can use a std::tuple here, or the like
> I need a function kind of like how SFML has shape.getPosition().x

Define a struct to hold the projected values.
Let the function that calculates the the projected values populate and return the struct

For example:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
#include <iostream>

struct projected_values {

    double x ;
    double y ;
    int light_level ;
    // etc.
};

struct camera { /* ... */ };

struct threed_model {

    projected_values get_projected( [[maybe_unused]] const camera& cam, int id ) const {

        // this is just a trivial example
        return projected_values{ 1.2*id, 3.4+id, 63 /* ... */ } ;
    }
    // ...
};

int main() {

    threed_model model ;
    camera camera_one ;

    // C++17 : structured binding
    // https://en.cppreference.com/w/cpp/language/structured_binding
    const auto[ x, y, light ] = model.get_projected( camera_one, 3 ) ;
    std::cout << "x: " << x << "   y: " << y << "   light level: " << light << '\n' ;

    // or in C++11
    const auto values = model.get_projected( camera_one, 3 ) ;
    std::cout << "x: " << values.x << "   y: " << values.y
              << "   light level: " << values.light_level << '\n' ;

    // or, use the values anonymously (and perhaps a bit less efficiently):
    std::cout << "x: " << model.get_projected( camera_one, 3 ).x
              << "   y: " << model.get_projected( camera_one, 3 ).y
              << "   light level: " << model.get_projected( camera_one, 3 ).light_level << '\n' ;
}

http://coliru.stacked-crooked.com/a/4e36786ba3f6a18b
Topic archived. No new replies allowed.