Solved

How to pass a member function to a template class?

Posted on 2002-05-23
1
189 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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
I could not build boost code, 10 104
Best book to learn C++ 4 87
c++ reading data from file into two dimensional array 3 129
Autosar OS Multicore Share Resources confusion ? 2 97
Templates For Beginners Or How To Encourage The Compiler To Work For You Introduction This tutorial is targeted at the reader who is, perhaps, familiar with the basics of C++ but would prefer a little slower introduction to the more ad…
IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
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 learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

679 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