get value attribute from other class

Good morning,
I instantiate class A and set its own attribute (example A.pippo) in main.cpp.
How can I get A.pippo from another class?
Thanks
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
#include <iostream>

struct A
{
    explicit A( int pippo = 0 ) : pippo_value(pippo) {}

    int pippo() const { return pippo_value ; }

    private: int pippo_value ;
};

struct B
{
    void set_pippo( const A& a ) // an object of type A is passed
    {
        v = a.pippo() ; // retrieve pippo from the passed object
        std::cout << "v set to a.pippo(). value == " << v << '\n' ;
    }

    private: int v = 0 ;
};

int main()
{
    const A a{234567} ;
    
    B b ;
    b.set_pippo(a) ; // pass the object as an argument
}

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