Object that contains 2 types of data

How do you create an object (like in the title) something more simple than a struct? I wanna know that cuz I'm writing a function that could return a boolean and an integer at the same time. That's all. Thanks in advance.
Last edited on
1) Use user defined object. Like class or struct
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
struct compound
{
    bool b;
    int  i;
};

compound foo(int x, bool u)
{
    compound result;
    result.b = u;
    result.i = x;
    return result;
}

int main()
{
    compound a = foo(5, true);
    if(a.b)
        std::cout << a.i;
}


2) use std::pair
1
2
3
std::pair<int, bool> x = std::make_pair(5, true);
if(x.second)
    std::cout << x.first;


3) use std::tuple
Pair and tuple. Cool! Thanks once again.
Topic archived. No new replies allowed.