/*
This program demonstrates the usage of a void pointer to do call backs in C++.
It's better to use a member function pointer but if you need to use a C library
that does call backs this will work.
*/

#include <iostream>
#include <string>

class obj_2;

class obj_1
{
private:

	/*
	Static wrapper function takes in a pointer to the appropriate instantiation
	of this object and calls the call_back function.
	*/
	static void call_back_wrapper(void * objectPtr)
	{
		obj_1 * this_class = (obj_1 *)objectPtr;
		this_class->call_back();
	}

	/*
	Can only do forward declaration with a pointer because with a pointer the
	compiler doesn't have to know how to instantiate the object(or how to set up
	all the junk in it's scope)
	*/
	obj_2 * Obj_2;

	//function that will get called back by Obj_2
	void call_back();

public:

	//function that will kick off the example
	void start();
};

class obj_2
{
public:
	/*
	The function that's going to do the call back. This function needs the
	object pointer to pass back to the wrapper and the function pointer of the
	wrapper function.
	*/
	void my_func(void (*fun_ptr)(void *), void * object_ptr);
};

void obj_1::start()
{
	Obj_2->my_func(call_back_wrapper, (void *)this);
}

void obj_1::call_back()
{
	std::cout << "zort\n";
}

void obj_2::my_func(void (*fun_ptr)(void *), void * object_ptr)
{
	fun_ptr(object_ptr);
}

int main(int argc, char * argv[])
{
	obj_1 Obj_1;
	Obj_1.start();

	return 0;
}
