Creating a Callback Function Pointer

Creating a Callback Function Pointer

Question:
I am using Microsoft Visual C++ 6.0 and am trying to create a callback function pointer to the public member of a class, but I keep getting the following error:

 error C2664: 'callback' : cannot convert parameter 1 from 'void' to 'void (__cdecl *)(void)'

Here is the code I am trying:

#include class bb  {public:	void callback(void (*f)());	void callback2();	bb();	virtual ~bb();};bb::bb(){}bb::~bb(){}void bb::callback(void (*f)()){	f();}void bb::callback2(){	cout << "callback2 called." << endl;}int main(int argc, char* argv[]){	bb *tmp = new bb;	tmp->callback(tmp->callback2());	return 0;}

TIA for any help.

Answer:
For starters, by appending the () to the end of callback2 in the following line:

 tmp->callback(tmp->callback2());

you are telling the compiler to call this function and then send the return value (in this case void) as an argument to tmp->callback. That definitely won’t work.

Unfortunately, removing the parentheses won’t make this work either. When C++ code calls a method on an object, is passes a hidden argument that is the pointer to the object (this). The downside to this is that you can’t treat class member functions and pointers to them the same way you’d handle regular functions.

Some things to try include making your member function static and checking out the .* and –>* (pointer to member) operators.

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes