Solved

How to pass a member function to a template class?

Posted on 2002-05-23
1
187 Views
Last Modified: 2010-04-02
How to pass a member function to a template class?

class widget
{
public:
 bool SomeFunc(void);
};

template<class T>
class MyTemplateClass
{
public:
 MyTemplateClass(????? MemberFunction);
 ??????? m_MemberFunction;
};

If I want to pass widget::SomeFunct to the MyTemplateClass, what syntax do I have to use.
Please provide working example.
0
Comment
Question by:AUDRABRETT7
1 Comment
 
LVL 22

Accepted Solution

by:
ambience earned 150 total points
ID: 7031149
class widget
{
public:
bool SomeFunc(void)
{
     cout << "somefunc" << endl;
     return true;
}

};


template<class T>
class MyTemplateClass
{
public:
typedef bool (T::*widget_ptr)();

MyTemplateClass(widget_ptr f)
{
     m_MemberFunction = f;
}

void DoIt()
{
     widget w;
     (w.*m_MemberFunction)();
}

widget_ptr m_MemberFunction;
};


int main()
{
   MyTemplateClass<widget> wig(widget::SomeFunc);
   wig.DoIt ();
   return 0;
}

Hope this helps
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

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

Suggested Solutions

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…
Often, when implementing a feature, you won't know how certain events should be handled at the point where they occur and you'd rather defer to the user of your function or class. For example, a XML parser will extract a tag from the source code, wh…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.

813 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

13 Experts available now in Live!

Get 1:1 Help Now