473,503 Members | 1,617 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 1717
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_=gcounter++;}

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

int id_;
};
std::vector<foov(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.begin(), V.end(), mem_fun(&B::print));
}

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
10046
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...
37
4945
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...
2
12663
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
2440
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...
23
7767
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...
3
3628
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'...
54
24438
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
4823
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...
20
2199
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...
10
3558
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...
0
7084
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...
0
7278
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
7328
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
6991
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...
0
7458
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5578
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
4672
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
3154
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
736
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.