?
Solved

How to pass a member function to a template class?

Posted on 2002-05-23
1
Medium Priority
?
194 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 600 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

  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 …
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
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.
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.
Suggested Courses

850 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