Problem when std::function refers to member function

  • Thread starter Thread starter ORF
  • Start date Start date
  • Tags Tags
    Function Member
Click For Summary
The discussion centers on the implementation of member functions within a class hierarchy in C++. A user seeks to enable a member function from one class (dev_b) to call a member function from another class (dev_a) within a composite class (master_t). The initial attempt to pass the member function directly resulted in a compilation error due to the lack of an object reference. The solution involves using a lambda function or std::bind to correctly reference the member function of dev_a. The suggested approach simplifies the code by allowing ShowB to be implemented in a manner similar to ShowA, thereby avoiding complex syntax. The conversation also emphasizes the importance of including necessary headers for the code to compile successfully. Overall, the key takeaway is the need for proper object referencing when dealing with member functions across different classes in C++.
ORF
Messages
169
Reaction score
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.

[CODE lang="cpp" title="Minimal working example" highlight="26"]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;
}[/CODE]

I naively tried first with obj.two.ShowB( obj.one.ShowA ); but I got the following error

[CODE lang="cpp" title="Error message"]error: no matching function for call to ‘dev_b::ShowB(<unresolved overloaded function type>)’
obj.two.ShowB( obj.one.ShowA );[/CODE]

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
 
Technology news on Phys.org
Your code worked fine for me, and produced this output:
Code:
dev_b::b = 2
dev_a::a = 1

Maybe I am missing the point of your question ... ?

It seems simpler to me to implement ShowB() similar to how ShowA() is implemented. That would eliminate the need for the convoluted code of line 26 - obj.two.ShowB( [&](){ obj.one.ShowA(); return;} );. This line truly deserves some commenting.

To make your code compile and run, I added the following lines at the top.
C++:
#include <iostream>
#include <functional>
using std::cout;
using std::function;
 
Last edited:
ORF said:
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?
That is correct. You can use a lambda to capture and call the object, or you can use std::bind, for example like
[CODE title="C++"]obj.two.ShowB( std::bind(&dev_a::ShowA, &obj.one) );[/CODE]
 
  • Like
Likes ORF and jim mcnamara
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 13 ·
Replies
13
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 22 ·
Replies
22
Views
3K
  • · Replies 11 ·
Replies
11
Views
2K
  • · Replies 36 ·
2
Replies
36
Views
5K
Replies
6
Views
2K
Replies
3
Views
2K
  • · Replies 57 ·
2
Replies
57
Views
4K
  • · Replies 6 ·
Replies
6
Views
12K
  • · Replies 4 ·
Replies
4
Views
3K