473,568 Members | 2,962 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Function pointer

Hello everyone,
In the following statements,

Expand|Select|Wrap|Line Numbers
  1. template <class R, class Tclass mem_fun_t : public unary_function
  2. <T*, R>
  3.  
  4. {
  5.  
  6. R (T::*pmf)()
  7. ....
  8. }
  9.  
1. I think pmf is a type of function pointer, the return type of the
function is R and the function is a member function of type (class) T.
Is my understanding correct?

2. If yes, what is the parameter list of the function? Empty parameter
list?

3. I doubt whether it is useful to define a function pointer with
empty parameter list -- too restricted.
thanks in advance,
George
Dec 10 '07 #1
3 1726
George2 wrote:
Hello everyone,
In the following statements,

Expand|Select|Wrap|Line Numbers
  1. template <class R, class Tclass mem_fun_t : public unary_function
  2. <T*, R>
  3. {
  4. R (T::*pmf)()
  5. ...
  6. }
  7.  

1. I think pmf is a type of function pointer,
With a semicolon added to the end of that line, pmf would be the name of a
member variable of mem_fun_t.
the return type of the function is R and the function is a member function
of type (class) T.
Yes.
2. If yes, what is the parameter list of the function? Empty parameter
list?
Yes.
3. I doubt whether it is useful to define a function pointer with
empty parameter list -- too restricted.
Well, it is restricted to functions that have exactly 0 parameters. If you
defined a pointer to function with 10 parameters, it would be restricted to
functions that take exactly 10 parameters.

Dec 10 '07 #2
George2 a écrit :
Hello everyone,
In the following statements,

Expand|Select|Wrap|Line Numbers
  1. template <class R, class Tclass mem_fun_t : public unary_function
  2. <T*, R>
  3. {
  4. R (T::*pmf)()
  5. ...
  6. }
  7.  

1. I think pmf is a type of function pointer, the return type of the
function is R and the function is a member function of type (class) T.
Is my understanding correct?
yes.
In brief it iss a pointer on a member fucntion of class T.
>
2. If yes, what is the parameter list of the function? Empty parameter
list?
Yes. No parameters.
>
3. I doubt whether it is useful to define a function pointer with
empty parameter list -- too restricted.
It is useful if you want to call a method of a class that takes no
parameters.

struct foo
{
static int gcounter=0;

foo(){id_=gcoun ter++;}

void bar(){std::cout <<"My id is "<<id_<<std::en dl;}

int id_;
};
std::vector<foo v(10);

std::for_each(v .begin(),v.end( ),std::mem_fun_ ref(&foo::bar)) ;
Of course, you could put parameters in a class and make it a wrapper
class of a more elaborate functor.

Another example from SGI:
struct B {
virtual void print() = 0;
};

struct D1 : public B {
void print() { cout << "I'm a D1" << endl; }
};

struct D2 : public B {
void print() { cout << "I'm a D2" << endl; }
};

int main()
{
vector<B*V;

V.push_back(new D1);
V.push_back(new D2);
V.push_back(new D2);
V.push_back(new D1);

for_each(V.begi n(), V.end(), mem_fun(&B::pri nt));
}

Michael
Dec 10 '07 #3
On 2007-12-10 06:48:11 -0500, George2 <ge************ *@yahoo.comsaid :
Hello everyone,
In the following statements,

Expand|Select|Wrap|Line Numbers
  1. template <class R, class Tclass mem_fun_t : public unary_function
  2. <T*, R>
  3. {
  4. R (T::*pmf)()
  5. ...
  6. }
  7.  

1. I think pmf is a type of function pointer,
No. It's a pointer to member function, which is not at all like a
pointer to (non-member) function.
the return type of the
function is R and the function is a member function of type (class) T.
Is my understanding correct?
Right.
>
2. If yes, what is the parameter list of the function? Empty parameter
list?
Yes.
>
3. I doubt whether it is useful to define a function pointer with
empty parameter list -- too restricted.
Read about what mem_fun_t is used for. The definition of pmf is exactly
what's needed.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

Dec 10 '07 #4

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

Similar topics

58
10077
by: jr | last post by:
Sorry for this very dumb question, but I've clearly got a long way to go! Can someone please help me pass an array into a function. Here's a starting point. void TheMainFunc() { // Body of code... TCHAR myArray; DoStuff(myArray);
37
4952
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...
2
12671
by: sushil | last post by:
+1 #include<stdio.h> +2 #include <stdlib.h> +3 typedef struct +4 { +5 unsigned int PID; +6 unsigned int CID; +7 } T_ID; +8 +9 typedef unsigned int (*T_HANDLER)(void); +10
27
2452
by: Marlene Stebbins | last post by:
I am experimenting with function pointers. Unfortunately, my C book has nothing on function pointers as function parameters. I want to pass a pointer to ff() to f() with the result that f() prints the return value of ff(). The code below seems to work, but I would appreciate your comments. Have I got it right? Does the function name "decay" to...
23
7784
by: bluejack | last post by:
Ahoy... before I go off scouring particular platforms for specialized answers, I thought I would see if there is a portable C answer to this question: I want a function pointer that, when called, can be a genuine no-op. Consider: typedef int(*polymorphic_func)(int param);
3
3637
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules' that takes arguments of type (void *) because the ADT must be able to deal with any type of data. In my actual code, I will code the function to take...
54
24461
by: John | last post by:
Is the following program print the address of the function? void hello() { printf("hello\n"); } void main() { printf("hello function=%d\n", hello); }
26
4831
by: aruna.mysore | last post by:
Hi all, I have a specific problem passing a function pointer array as a parameter to a function. I am trying to use a function which takes a function pointer array as an argument. I am too sure about the syntax of calling the same. #include <stdio.h> void fp1()
20
2201
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to think my way through what I want to do now. I don't know why - I'm fine with most other aspects of the language, but my brain goes numb when I'm...
10
3584
by: Richard Heathfield | last post by:
Stephen Sprunk said: <snip> Almost. A function name *is* a pointer-to-function. You can do two things with it - copy it (assign its value to an object of function pointer type, with a cast if necessary but preferably to one of the /right/ function pointer
0
7605
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...
0
7917
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. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
1
5501
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...
0
5217
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...
0
3651
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...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1207
muto222
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.