/*
This program demonstrates the usage of a member function pointer in C++ to do
call backs.
*/

#include <iostream>
#include <string>

class obj_2;

class obj_1
{
private:

	/*
	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(obj_1 & Obj_1, void (obj_1::*memfun_ptr)());
};

void obj_1::start()
{
	void (obj_1::*memfun_ptr)() = &obj_1::call_back;
	Obj_2->my_func(*this, memfun_ptr);
}

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

void obj_2::my_func(obj_1 & Obj_1, void (obj_1::*memfun_ptr)())
{
	(Obj_1.*memfun_ptr)();
}

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

	return 0;
}
