typedef and function pointers (with instances)

Ok this should be pretty straight-forward imo but for some reason I can't get anything I try to work.

Can someone explain why this code will not compile or another way of doing this function pointer callback thing.

My goal is to send this function pointer with some request data to another thread. Once the thread has finished with the data it sends that data to the function presented from the function pointer. The pointer should point to the instance method (there is only ever 1 instance).

Just trying to use standard c++11. (and learn ofc).

The example is barebones but produces my problem.

Main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include<iostream>
#include"ClassA.h"


int main()
{
	ClassA *classa = new ClassA();
	classa->invokeCallback(&classa->callbackFunc, "some initial data");

	std::cin.get();
    return 0;
}


ClassA.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#include<map>
#include<string>

typedef void(*tFunc)(std::string); // function pointer type

typedef std::map<tFunc*, std::string> tFuncMap; // need tFunc typedef for map typedef

class ClassA
{
	public:
		void callbackFunc(std::string data); // Callback func
		void invokeCallback(tFunc t, std::string data);
};


ClassA.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "stdafx.h"
#include "ClassA.h"
#include<iostream>

void ClassA::callbackFunc(std::string data)
{
	// do some stuff
	std::cout << "Did some stuff with: " + data;
};

void invokeCallback(tFunc t, std::string data)
{
	t(data);
};


classa->invokeCallback(&classa->callbackFunc, "some initial data");
This is the trouble... Can't seem to use instances and using the static decleration the types mis-match? Because it's in the class scope maybe? but how fix :(

Thanks for any advice. Just want to learn really, how i can use callback functions in this way.

Also any more efficient alternatives for my idea would be helpful.

(Idea is to have a seperate thread with a queue system, the main thread will add to queue even if other thread is working on another queue item. Main thread would retrieve results in a map type array with tickets to match request with result data).

Thanks!
--------------

EDIT:
So I have worked out how to do this while passing an instance pointer separately like so,

Main
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdafx.h"
#include<iostream>
#include"ClassA.h"


int main()
{
	ClassA *classa = new ClassA();
	classa->invokeCallback(classa, & ClassA::callbackFunc, "some initial data");

	std::cin.get();
    return 0;
}


ClassA Header
1
2
3
4
5
6
7
8
9
10
11
12
13
#pragma once
#include<map>
#include<string>

class ClassA
{
	public:
		typedef void(ClassA::*tFunc)(std::string); // function pointer type

		void callbackFunc(std::string data); // Callback func
		void invokeCallback(ClassA*c, tFunc t, std::string data);
};


Class A
1
2
3
4
5
6
7
8
9
10
11
12
13
#include "stdafx.h"
#include "ClassA.h"
#include<iostream>

void ClassA::callbackFunc(std::string data)
{
	// do some stuff
	std::cout << "Did some stuff with: " + data;
}
void ClassA::invokeCallback(ClassA*c, tFunc t, std::string data)
{
	(c->*(t))(data);
};


Now the question is, how can I pass a direct pointer to the instanced method? Rather than passing the static pointer, can't i add the static method pointer to the instance pointer? how would that work gracefully?

Maybe related but also, how could i make a generic reference to a pointer type rather than specifying the class pointer? (ClassA*c)

Thanks!
Last edited on
You may pass a function object:

http://www.cplusplus.com/reference/functional/

Using bind() you can pass a member function as well as a free function.
Thanks! Not quite what I want for my application but never the less it gave me an idea and I'm now making good progress on the concept :). And ofc now I know about functional objects :).
Maybe related but also, how could i make a generic reference to a pointer type rather than specifying the class pointer? (ClassA*c)


Since you're using a class method to invoke the callback function, I don't see why you're passing a pointer to an instance at all:

1
2
3
4
void ClassA::invokeCallback(tFunc t, std::string data)
{
	(this->*(t))(data);
}
Last edited on
Topic archived. No new replies allowed.