Solved

Function pointer not working too well...

Posted on 2009-07-07
5
171 Views
Last Modified: 2012-05-07
Hi,

Please see the code below - no message box is showing despite me asking the computer very nicely... :(

Thanks,
Uni
template<typename TListenerClassName>

class CEventRaiser{

public:

	typedef void (TListenerClassName::*ListenerClassMember)();

	void initialiseEventRaiser(TListenerClassName *aListenerClassName, ListenerClassMember aListenerClassMember){

		listenerClassName=aListenerClassName;

		listenerClassMember=aListenerClassMember;

	}

	void raiseEvent(){

		listenerClassName->*listenerClassMember;

	}

private:

	TListenerClassName *listenerClassName;

	ListenerClassMember listenerClassMember;

};
 
 

class CSample{

public:

	void init(){

		eventRaiser.initialiseEventRaiser(this, &CSample::listener);

		eventRaiser.raiseEvent();

	}

	void listener(){

		MessageBox(0, L"H", L"", 0);

	}

	CEventRaiser<CSample> eventRaiser;

};
 
 

int main(){

	CSample sample;

	sample.init();

	...

}

Open in new window

0
Comment
Question by:Unimatrix_001
  • 3
5 Comments
 
LVL 40

Accepted Solution

by:
mrjoltcola earned 500 total points
ID: 24799759
Try calling it like:

void raiseEvent(){
                (listenerClassName->*listenerClassMember)();
        }
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24799773
Ooops...
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
ID: 31600903
Thank you. :)
0
 
LVL 40

Expert Comment

by:evilrix
ID: 24801257
heh. I guess I went to be a little too early last night Uri :)
0
 
LVL 3

Author Comment

by:Unimatrix_001
ID: 24803742
I went to bed waaay too late. ;)
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In days of old, returning something by value from a function in C++ was necessarily avoided because it would, invariably, involve one or even two copies of the object being created and potentially costly calls to a copy-constructor and destructor. A…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to pass data into a function in C++. This is one step further in using functions. Instead of only printing text onto the console, the function will be able to perform calculations with argumentents given by the user.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

26 Experts available now in Live!

Get 1:1 Help Now