Solved

How to pass a member function to a template class?

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

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

Errors will happen. It is a fact of life for the programmer. How and when errors are detected have a great impact on quality and cost of a product. It is better to detect errors at compile time, when possible and practical. Errors that make their wa…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
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.

864 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

21 Experts available now in Live!

Get 1:1 Help Now