473,387 Members | 1,481 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

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 1716
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
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
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
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
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
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
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
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
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
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
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
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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
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...

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.