Solved

Function pointer not working too well...

Posted on 2009-07-07
5
170 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
Comment Utility
Try calling it like:

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

Author Comment

by:Unimatrix_001
Comment Utility
Ooops...
0
 
LVL 3

Author Closing Comment

by:Unimatrix_001
Comment Utility
Thank you. :)
0
 
LVL 40

Expert Comment

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

Author Comment

by:Unimatrix_001
Comment Utility
I went to bed waaay too late. ;)
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Article by: SunnyDark
This article's goal is to present you with an easy to use XML wrapper for C++ and also present some interesting techniques that you might use with MS C++. The reason I built this class is to ease the pain of using XML files with C++, since there is…
  Included as part of the C++ Standard Template Library (STL) is a collection of generic containers. Each of these containers serves a different purpose and has different pros and cons. It is often difficult to decide which container to use and …
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will learn how to user default arguments when defining functions. This method of defining functions will be contrasted with the non-default-argument of defining functions.

771 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

11 Experts available now in Live!

Get 1:1 Help Now