Solved

How to pass a member function to a template class?

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

Free Trending Threat Insights Every Day

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

Join & Write a Comment

This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
Container Orchestration platforms empower organizations to scale their apps at an exceptional rate. This is the reason numerous innovation-driven companies are moving apps to an appropriated datacenter wide platform that empowers them to scale at a …
The viewer will learn how to use the return statement in functions in C++. The video will also teach the user how to pass data to a function and have the function return data back for further processing.
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.

758 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