- #1
ORF
- 170
- 18
Hi,
I have a class master_t which is composed by two other classes, dev_a, dev_b. I would like that a member function from the dev_b object (within master_t) could use a member function of dev_a object (within master_t). This is a minimal working code, where line 26 implements this feature.
I naively tried first with obj.two.ShowB( obj.one.ShowA ); but I got the following error
I have two questions:
a) The cause of the error is because when passing the function like obj.two.ShowB( obj.one.ShowA ); is missing the reference to the actual object obj.one?
b) Would it be better to implement the function dev_b::ShowB as member function of master_t instead, so it can have access to both dev_a and dev_b objects?
Thank you for your time. Any comment is welcome.
Cheers,
ORF
I have a class master_t which is composed by two other classes, dev_a, dev_b. I would like that a member function from the dev_b object (within master_t) could use a member function of dev_a object (within master_t). This is a minimal working code, where line 26 implements this feature.
Minimal working example:
class dev_a
{
public:
int a = {1};
void ShowA( ){ std::cout << "dev_a::a = " << a << '\n'; return; }
};
class dev_b
{
public:
int b = {2};
void ShowB( std::function<void(void)> ShowA ){ std::cout << "dev_b::b = " << b << '\n'; ShowA(); return; }
};
class master_t
{
public:
dev_a one;
dev_b two;
};
// Driver code
int main()
{
master_t obj;
obj.two.ShowB( [&](){ obj.one.ShowA(); return;} );
return 0;
}
I naively tried first with obj.two.ShowB( obj.one.ShowA ); but I got the following error
Error message:
error: no matching function for call to ‘dev_b::ShowB(<unresolved overloaded function type>)’
obj.two.ShowB( obj.one.ShowA );
I have two questions:
a) The cause of the error is because when passing the function like obj.two.ShowB( obj.one.ShowA ); is missing the reference to the actual object obj.one?
b) Would it be better to implement the function dev_b::ShowB as member function of master_t instead, so it can have access to both dev_a and dev_b objects?
Thank you for your time. Any comment is welcome.
Cheers,
ORF