Click here to Skip to main content
15,904,415 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: Compile Error Actual linking error ....Can someone help. Pin
FISH7862-Mar-09 8:58
FISH7862-Mar-09 8:58 
GeneralRe: Compile Error Actual linking error ....Can someone help. Pin
Jonathan Davies2-Mar-09 12:23
Jonathan Davies2-Mar-09 12:23 
GeneralRe: Compile Error Actual linking error ....Can someone help. Pin
FISH7862-Mar-09 16:52
FISH7862-Mar-09 16:52 
GeneralRe: Compile Error Actual linking error ....Can someone help. Pin
Jonathan Davies3-Mar-09 0:25
Jonathan Davies3-Mar-09 0:25 
GeneralRe: Compile Error Actual linking error ....Can someone help. Pin
FISH7863-Mar-09 2:49
FISH7863-Mar-09 2:49 
QuestionFunction Pointer Pin
dehseth2-Mar-09 7:38
dehseth2-Mar-09 7:38 
AnswerRe: Function Pointer Pin
Richard Andrew x642-Mar-09 7:48
professionalRichard Andrew x642-Mar-09 7:48 
AnswerRe: Function Pointer Pin
Stuart Dootson2-Mar-09 8:02
professionalStuart Dootson2-Mar-09 8:02 
A non-static method pointer is the same as any function pointer that needs parameters - you need to tell the caller what those parameters are.

So, you could do this:

class R
{
public:
	typedef void (A::*Method)();
	struct MethodCall { Method fn; A obj; };
	MethodCall onClick;

	void Do()
	{
		Method m = onClick.fn;
		A& a = onClick.obj;
      
		((a).*(m))();
	}
};

int main(int argc, char* argv[])
{
	
	A a;

	R r;
	r.onClick.obj = a;
	r.onClick.fn = &A::run; //wont work 
	r.Do();
}


But then if you wanted to use ok as a handler as well, you'd need to add a separate data member for that.

Getting around all that is the raison d'etre for Boost.Function[^] (also in std::tr1::function in VC2008, I believe). With that (and a smidgen of Boost.Lambda[^]), I'd write your onClick handler like this:

#include <iostream>
#include <boost/function.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>

class A
{
public:
   void run(void) { std::cout << "A::run\n"; }
};

class R
{
public:
   boost::function<void()> onClick;

   void Do()
   {
      onClick();
   }
};


void ok()
{
   std::cout << "ok\n";
}

int main(int argc, char* argv[])
{

   A a;

   R r;
   r.onClick = ok;
   r.Do();
   using namespace boost::lambda;
   r.onClick = bind(&A::run, var(a));
   r.Do();
}


If you then want multiple handlers, Boost.Signals[^] is very useful as well!

[edit]It would be remiss of me not to point out that Boost.Function is compatible with the STL binders such as mem_fun[^] - but as Boost.Lambda is so much cooler, I hope you'll forgive me Smile | :) [/edit]

Java, Basic, who cares - it's all a bunch of tree-hugging hippy cr*p

GeneralRe: Function Pointer Pin
dehseth2-Mar-09 8:14
dehseth2-Mar-09 8:14 
GeneralRe: Function Pointer Pin
Stuart Dootson2-Mar-09 8:17
professionalStuart Dootson2-Mar-09 8:17 
QuestionRe: Function Pointer Pin
CPallini2-Mar-09 10:17
mveCPallini2-Mar-09 10:17 
GeneralRe: Function Pointer Pin
Richard Andrew x642-Mar-09 9:38
professionalRichard Andrew x642-Mar-09 9:38 
GeneralRe: Function Pointer [modified] Pin
Stuart Dootson2-Mar-09 10:02
professionalStuart Dootson2-Mar-09 10:02 
GeneralRe: Function Pointer Pin
Rajesh R Subramanian2-Mar-09 19:59
professionalRajesh R Subramanian2-Mar-09 19:59 
AnswerRe: Function Pointer Pin
Perisic, Aleksandar2-Mar-09 8:45
Perisic, Aleksandar2-Mar-09 8:45 
GeneralRe: Function Pointer Pin
Perisic, Aleksandar2-Mar-09 9:08
Perisic, Aleksandar2-Mar-09 9:08 
AnswerRe: Function Pointer [modified] Pin
Perisic, Aleksandar2-Mar-09 11:48
Perisic, Aleksandar2-Mar-09 11:48 
Question[Message Deleted] Pin
Davitor2-Mar-09 5:58
Davitor2-Mar-09 5:58 
AnswerRe: File handaling problem Pin
Richard Andrew x642-Mar-09 6:12
professionalRichard Andrew x642-Mar-09 6:12 
GeneralRe: File handaling problem Pin
Ric Ashton2-Mar-09 6:31
Ric Ashton2-Mar-09 6:31 
GeneralRe: File handaling problem Pin
Ric Ashton2-Mar-09 6:33
Ric Ashton2-Mar-09 6:33 
GeneralRe: File handaling problem Pin
Davitor2-Mar-09 6:45
Davitor2-Mar-09 6:45 
GeneralRe: File handaling problem Pin
Davitor2-Mar-09 6:42
Davitor2-Mar-09 6:42 
GeneralRe: File handaling problem Pin
Eytukan2-Mar-09 6:56
Eytukan2-Mar-09 6:56 
GeneralRe: File handaling problem Pin
Ric Ashton2-Mar-09 6:59
Ric Ashton2-Mar-09 6:59 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.