C++ Function Pointers

C

chetan

How to initialise a function pointer for any particular function in
C++ ?

so that it would clearly specify difference between the functions that
are called by different objects of that class ...
i.e
instead of

a.function();
or
b.function();

use of this -->
int (*funcptr)(void); ???? How ?
 
J

John Harrison

chetan said:
How to initialise a function pointer for any particular function in
C++ ?

so that it would clearly specify difference between the functions that
are called by different objects of that class ...
i.e
instead of

a.function();
or
b.function();

use of this -->
int (*funcptr)(void); ???? How ?

What are you asking? How can a function pointer 'specify the difference
between the functions that are called by different objects of that class'?
What does that mean?

I think you're going to have to ask again and choose your words a little
more carefully this time. I'm sure you know what you mean but I don't think
its very clear to anyone else. Sometimes it helps to post some pseudo code
to more clearly express what you want to do.

john
 
A

Arijit

How to initialise a function pointer for any particular function in
C++ ?

so that it would clearly specify difference between the functions that
are called by different objects of that class ...
i.e
instead of

a.function();
or
b.function();

use of this -->
int (*funcptr)(void); ???? How ?

int (*funcptr)(); Don't use void.

I don't clearly understand what you are asking for, but my guess is you
want to call a class member function by a function pointer, like you can
do for ordinary functions. There is a pointer-to-member-function that you
can use. Something like: int (C::*mem_func_ptr)() where C is the class.

-Arijit
 
T

Thomas Matthews

chetan said:
How to initialise a function pointer for any particular function in
C++ ?
You assign the name of the function to the pointer:
function_pointer = Function_Name;

Also be aware that you can only assign functions that match the
signature of the function pointer.

so that it would clearly specify difference between the functions that
are called by different objects of that class ...
i.e
instead of

a.function();
or
b.function();

use of this -->
int (*funcptr)(void); ???? How ?
Looks like you want a pointer to a member function.
Read the FAQ for a good explanation:
http://www.parashift.com/c++-faq-lite/pointers-to-members.html


--
Thomas Matthews

C++ newsgroup welcome message:
http://www.slack.net/~shiva/welcome.txt
C++ Faq: http://www.parashift.com/c++-faq-lite
C Faq: http://www.eskimo.com/~scs/c-faq/top.html
alt.comp.lang.learn.c-c++ faq:
http://www.comeaucomputing.com/learn/faq/
Other sites:
http://www.josuttis.com -- C++ STL Library book
 
R

Ron Natalie

Thomas said:
Also be aware that you can only assign functions that match the
signature of the function pointer.
Not SIGNATURE but pointers of the same TYPE.

Sorry to be pendantic, but they're not quite the same thing.
SIGNATURE applies to the name and args and defines overloading
instances. The function args and return types are part of the
function TYPE. You can only assign pointers to functions of
the same type.
 
J

JKop

chetan posted:
How to initialise a function pointer for any particular function in
C++ ?

so that it would clearly specify difference between the functions that
are called by different objects of that class ...
i.e
instead of

a.function();
or
b.function();

use of this -->
int (*funcptr)(void); ???? How ?


Here's two functions:

int Blah(double pig)
{
return pig - 5;
}


char Bleck(int k)
{
return k % 9;
}

And here's how you call them using function pointers:

int main()
{
int (*p_Blah)(double); //define the pointer variables
char (*p_Bleck)(int);

p_Blah = Blah; //Assign an address to them
p_Bleck = Bleck;

//NB: When you write a fuction's name without
//parenthesis, you've got the function's address
}



-JKop
 
B

Bob Hairgrove

chetan posted:



Here's two functions:

int Blah(double pig)
{
return pig - 5;
}


char Bleck(int k)
{
return k % 9;
}

And here's how you call them using function pointers:

int main()
{
int (*p_Blah)(double); //define the pointer variables
char (*p_Bleck)(int);

p_Blah = Blah; //Assign an address to them
p_Bleck = Bleck;

//NB: When you write a fuction's name without
//parenthesis, you've got the function's address
}

You haven't called anything. Besides, the OP really wanted to know
about pointers to member functions, not regular functions.
 
D

David Lindauer

There are two types of function pointers.

1) pointer to a non-member function

int nn(int b) ; // define a function
int (*aa)(int b) ; // define a pointer
aa = nn ; // assign the function to the pointer
int bb = (*aa)(5) ; // call the function

2) pointer to a member function

struct ss {
int nn(int b) ; // define member function
} ;
int (ss::*aa)(int b) ; // define pointer to it, this is NOT a class
member variable the way it is written
aa = ss::nn ; // assign function to the pointer
struct ss *t = new ss ; // make a structure variable
int c = t->*aa(5) ; // call the member function , can also use ".*" for
non-pointer instances
 
J

JKop

David Lindauer posted:
There are two types of function pointers.

1) pointer to a non-member function

int nn(int b) ; // define a function

That's "declare a function".
int (*aa)(int b) ; // define a pointer
aa = nn ; // assign the function to the pointer
int bb = (*aa)(5) ; // call the function

Yes; but 99% of the time, that's written as:

int bb = aa(5);
 
D

David Lindauer

JKop said:
David Lindauer posted:


That's "declare a function".


Yes; but 99% of the time, that's written as:

int bb = aa(5);

maybe to you... I prefer the former because it makes it very plain you
are calling through a variable. Such things make a huge difference when
you hand off a program to someone else... they can immediately see what
is going on.

David
 
J

JKop

David Lindauer posted:
maybe to you... I prefer the former because it makes it very plain you
are calling through a variable. Such things make a huge difference when
you hand off a program to someone else... they can immediately see what
is going on.

David


But at the same time, the asterisk is redundant.

Take a variable/object for example. If it's type is "int*", then when you
stick an asterisk in front of it, you've got an expression of type "int".
Like so:

int* p = ...;

*p;


Now take a pointer to a function. If it's type is "int (*)(double)", then
when you stick an asterisk in front of it, you've got an expression of type
"int (*)(double)". Nothing has changed - the asterisk is redundant.


Anyway; as regards making it clear that it's being called "through a
pointer", I don't see the relevance - it doesn't have to be worked with any
differently. Take pointer variables for example, I usually prefix "p_" to
their name to clearly indicate that it's a pointer, because pointers are
work with differently to normal variables/objects. But this isn't so for
pointers to functions, they work exactly the same - there's no need to say
"Look at me, I'm really a pointer, keep it in mind".


Also, I'd prefer a reference to a function wherever possible.


-JKop
 
D

David Lindauer

JKop said:
David Lindauer posted:


But at the same time, the asterisk is redundant.

Take a variable/object for example. If it's type is "int*", then when you
stick an asterisk in front of it, you've got an expression of type "int".
Like so:

int* p = ...;

*p;

Now take a pointer to a function. If it's type is "int (*)(double)", then
when you stick an asterisk in front of it, you've got an expression of type
"int (*)(double)". Nothing has changed - the asterisk is redundant.

Anyway; as regards making it clear that it's being called "through a
pointer", I don't see the relevance - it doesn't have to be worked with any
differently. Take pointer variables for example, I usually prefix "p_" to
their name to clearly indicate that it's a pointer, because pointers are
work with differently to normal variables/objects. But this isn't so for
pointers to functions, they work exactly the same - there's no need to say
"Look at me, I'm really a pointer, keep it in mind".

Also, I'd prefer a reference to a function wherever possible.

-JKop

redundancy is good. It makes it easier to pick up on nuances of code that
might otherwise take some studying. I know as a coder it is fun to pick the
optimal way of expressing yourself, but the code has a *much* longer life than
just the few months you spend coding it, and you may not be there to deal with
it throughout that life.

David
 

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
474,183
Messages
2,570,967
Members
47,517
Latest member
Andres38A1

Latest Threads

Top