O
Oplec
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");
}
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");
}