473,793 Members | 2,865 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Passing member function pointers are template parameters


I am stuck with a syntactical issue and would appreciate some help:

I have a class with a templated mem fn like so:

class A
{
};

template<typena me Ret, typename P1, Ret (A::*fp)(P1*)>
void A::somefunc()
{
P1* p = getP1FromSomewh ere();
someotherfunc(p , fp);
}

// should I re-write the whole template shebang for someotherfunc?
// I tried (probably w/o understanding completely)
template<typena me X, typename T>
void A::someotherfun c(X* p1, T func)
{
// fails with compiler error the statement does not evaluate to a
function being called
// with one parameter
func(p1);
}

What is the correct way to pass function pointers in a cascade?

P.s: I am mucking around with legacy code and although there might be
better ways of acheiving what I want, I am not in a position to make
whole-sale code changes

Nov 6 '06 #1
3 2055
Dilip wrote:
I am stuck with a syntactical issue and would appreciate some help:

I have a class with a templated mem fn like so:

class A
{
};

template<typena me Ret, typename P1, Ret (A::*fp)(P1*)>
void A::somefunc()
{
P1* p = getP1FromSomewh ere();
someotherfunc(p , fp);
}

// should I re-write the whole template shebang for someotherfunc?
// I tried (probably w/o understanding completely)
template<typena me X, typename T>
void A::someotherfun c(X* p1, T func)
{
// fails with compiler error the statement does not evaluate to a
function being called
// with one parameter
func(p1);
}

What is the correct way to pass function pointers in a cascade?
Yes, but it's not a correct way to use a pointer to member. You need
an instance of the class. Did you mean to write

(this->*func)(p1);

(or some such)?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Nov 6 '06 #2

Dilip wrote:
I am stuck with a syntactical issue and would appreciate some help:

I have a class with a templated mem fn like so:

class A
{
};

template<typena me Ret, typename P1, Ret (A::*fp)(P1*)>
void A::somefunc()
{
P1* p = getP1FromSomewh ere();
someotherfunc(p , fp);
}

// should I re-write the whole template shebang for someotherfunc?
// I tried (probably w/o understanding completely)
template<typena me X, typename T>
void A::someotherfun c(X* p1, T func)
{
// fails with compiler error the statement does not evaluate to a
function being called
// with one parameter
func(p1);
}

What is the correct way to pass function pointers in a cascade?

P.s: I am mucking around with legacy code and although there might be
better ways of acheiving what I want, I am not in a position to make
whole-sale code changes
Not 100% sure about your class requirements as the defintion is
empty...but here is what I could come up with based on the function
sgnatures you had..

class P1
{
};

class P
{
public:
void Pfunc(P1* p1_ptr)
{
};
};

class A
{
public:
template<typena me Ret, typename tP1, typename tP2, Ret
(tP1::*fp)(tP2* )>
void somefunc(tP1* p) ;

template<typena me X, typename T>
void someotherfunc(X * p1, T func);
};
template<typena me Ret, typename tP1, typename tP2, Ret
(tP1::*fp)(tP2* )>
void A::somefunc(tP1 * p)
{
someotherfunc(p , fp);
}
template<typena me X, typename T>
void A::someotherfun c(X* p, T func)
{
P1* p1new = new P1;
(p->*func)(p1new );
delete p1new;
}

Nov 6 '06 #3
Victor Bazarov wrote:
What is the correct way to pass function pointers in a cascade?

Yes, but it's not a correct way to use a pointer to member. You need
an instance of the class. Did you mean to write

(this->*func)(p1);
Thanks! Thats exactly what I was looking for.

Nov 6 '06 #4

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

Similar topics

6
2306
by: Garma | last post by:
According to what I have learnt so far, instantiating global objects should be the last resort. Is there any reasons why so? Sometimes some objects or their pointers have to be shared among several other objects, I'd like to know how this is handled commonly. Could someone elaborate on this? Another situation is some objects could be shared among several threads. How to handle it commonly?
2
1470
by: Dave | last post by:
Hello all, At the line marked "Problem here???" below, I get successful compilation on one platform and failure on another. What does the Standard say about this? Is this a correct program? May a member function be called through a pointer when some parameter default values are accepted (i.e. not all parameters are passed)? Thanks, Dave
37
5021
by: Ben | last post by:
Hi, there. Recently I was working on a problem where we want to save generic closures in a data structure (a vector). The closure should work for any data type and any method with pre-defined signature. When developing this lib, I figured that the pointer-to-member-function, although seemingly an attractive solution, does not work well for us.
3
1959
by: ma740988 | last post by:
Consider the template member function that generates a random number within a range. #include <cstdlib> #include <ctime> #include <iostream> template<typename T> T Random(T bottom, T top) {
11
4433
by: cps | last post by:
Hi, I'm a C programmer taking my first steps into the world of C++. I'm currently developing a C++ 3D graphics application using GLUT (OpenGL Utility Toolkit written in C) for the GUI components. The application is built around a "World" object that contains a "GUI" object that is a C++ wrapper around GLUT. What I'd like to do is pass a pointer to a member function in the World class to function in the GUI
3
3068
by: ryan.mitchley | last post by:
Hi all I have a class (cPort) that is designed to receive objects and, depending on the type, call a handler (callback) in any descendant of a cProcessBlock class. Callback functions take a shared_ptr<cBaseas a parameter, and return void. The code was working fine, although I have encountered problems (under a Microsoft compiler, of course - VC 8.0) when I attempt to add callbacks to a class with multiple inheritance. I hate multiple
18
2880
by: tbringley | last post by:
I am a c++ newbie, so please excuse the ignorance of this question. I am interested in a way of having a class call a general member function of another class. Specifically, I am trying to write an ordinary differential equation class that would solve a general equation in the form: dx/dt = f(x,t). The ode class shouldn't know anything about f, except how to call it.
12
7874
by: claudiu | last post by:
Hi, I'll go straight to the first question. Why does the code below compile? void f(int i = 0); int main() { (&f)();
23
2157
by: osama178 | last post by:
Let's say I have this code -------------------- class GenericQueue { public: bool Pop(void*& refToPtr); // //--------------------(1) };
0
9671
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9518
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10433
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
7538
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
6777
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5436
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4112
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 we have to send another system
2
3720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2919
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.