473,471 Members | 1,814 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Template parameter to member function problem

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

Jul 19 '05 #1
1 1907
"Oplec" <an*******@anonymous.cp> 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
Jul 19 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: CoolPint | last post by:
As a self-exercise, I am trying to write a generic Priority Queue, which would store any type and and accept any user-definable "priority" function. After much tinkering, I came up with...
7
by: Lionel B | last post by:
Greetings. The following code compiles ok and does what I'd expect it to do: ---------- START CODE ---------- // test.cpp
3
by: David Komanek | last post by:
Hi all, I am trying to learn more about how to use g++/Cygwin to produce dll files on WinXP. And I have a problem which at the first look seems to be an obvious dll-export problem, but I don't...
2
by: Bjoern Knafla | last post by:
Hi! I am trying to specialize a member function of a template class (see code below). The moment the header with the "template class definition" and "template class member function...
9
by: tropostropos | last post by:
On Solaris, using the Sun compiler, I get annoying warnings from the following code. The problem is that I am passing a C++ member function pointer to the C library function qsort. Is there a...
12
by: mlimber | last post by:
This is a repost (with slight modifications) from comp.lang.c++.moderated in an effort to get some response. I am using Loki's Factory as presented in _Modern C++ Design_ for message passing in...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better luck :) The standard explanation is that pointer...
3
by: Fei Liu | last post by:
Hello, We all know that a template function can automatically deduce its parameter type and instantiate, e.g. template <tpyename T> void func(T a); func(0.f); This will cause func<floatto...
4
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs calculations. Some calculations need only be performed if the...
6
by: Gaijinco | last post by:
I'm trying to do a template class Node. My node.hpp is: #ifndef _NODE_HPP_ #define _NODE_HPP_ namespace com { namespace mnya { namespace carlos { template <typename T>
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.