Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Template parameter to member function problem

Reply
Thread Tools

Template parameter to member function problem

 
 
Oplec
Guest
Posts: n/a
 
      11-03-2003
Hi, I have a class template that takes an argument to a class and an
argument to a member function of the given class. I have to use
template<class T, void (T::*MF)()> but would like to use something along
the lines of template<class T, class MF>. Is that possible? Below is the
code of my currently working method.

#include <cstdlib>
#include <iostream>

template<class C, void (C::*MF)()>
class A {
public:
A(C& o) : object_(o) { }
void operator()() { (object_.*MF)(); }

private:
C& object_;
};

struct B {
void f() { std::cout << "B::f()\n"; }
};

int main()
{
B b;
A<B,&B::f> aa(b);

// output: B::f()
aa();

system("PAUSE");
}

 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      11-03-2003
"Oplec" <(E-Mail Removed)> wrote...
> Hi, I have a class template that takes an argument to a class and an
> argument to a member function of the given class. I have to use
> template<class T, void (T::*MF)()> but would like to use something along
> the lines of template<class T, class MF>. Is that possible?


Of course it's possible. Have you tried?

The difference is that 'void (T::*MF)()' is a non-type parameter.
You want a _type_ parameter (who knows why?). Since you're going to
remove the way to pass the actual pointer, you have to change the
interface of the class A so that the pointer is passed elsewhere.

> Below is the
> code of my currently working method.
>
> #include <cstdlib>
> #include <iostream>
>
> template<class C, void (C::*MF)()>
> class A {
> public:
> A(C& o) : object_(o) { }
> void operator()() { (object_.*MF)(); }
>
> private:
> C& object_;
> };
>
> struct B {
> void f() { std::cout << "B::f()\n"; }
> };
>
> int main()
> {
> B b;
> A<B,&B::f> aa(b);
>
> // output: B::f()
> aa();
>
> system("PAUSE");
> }


Before we can tell you the solution, you have to tell us
the problem. A<B,&B::f> is the current way to pass the
pointer to a member of B. How would you like to pass it
instead? You could do

A<B> aa(b, &B::f);

is that what you need? You could also do

aa(&B::f);

is that what you need? Don't keep us guessing.

Victor


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the template member function of a template in the memberfunction of another template class? Peng Yu C++ 3 10-26-2008 03:51 PM
Passing a member function as a parameter of a member function Azdo C++ 2 03-06-2007 11:21 AM
specialized member function takes precedence over generic template member function bluekite2000@gmail.com C++ 1 07-20-2005 08:58 PM
parse error in gcc but success in vc.net, call a non_template class's template member function from a template class's member function! ken C++ 2 06-28-2005 06:57 AM
Passing a pointer to member function as a parameter to another member function Newsgroup - Ann C++ 5 07-30-2003 02:54 AM



Advertisments