Function pointer

G

George2

Hello everyone,


In the following statements,

Code:
template <class R, class T> class mem_fun_t : public unary_function
<T*, R>

{

R (T::*pmf)()
....
}

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
 
R

Rolf Magnus

George2 said:
Hello everyone,


In the following statements,

Code:
template <class R, class T> class mem_fun_t : public unary_function
<T*, R>

{

R (T::*pmf)()
...
}

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.
 
M

Michael DOUBEZ

George2 a écrit :
Hello everyone,


In the following statements,

Code:
template <class R, class T> class mem_fun_t : public unary_function
<T*, R>

{

R (T::*pmf)()
...
}

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

Michael
 
P

Pete Becker

Hello everyone,


In the following statements,

Code:
template <class R, class T> class mem_fun_t : public unary_function
<T*, R>

{

R (T::*pmf)()
...
}

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,772
Messages
2,569,593
Members
45,112
Latest member
VinayKumar Nevatia
Top