473,395 Members | 1,623 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,395 software developers and data experts.

C++ Function Pointers

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 ?
Jul 22 '05 #1
11 1717

"chetan" <ro****@indiatimes.com> wrote in message
news:4c**************************@posting.google.c om...
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
Jul 22 '05 #2
ro****@indiatimes.com (chetan) wrote in message news:<4c**************************@posting.google. com>...
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
Jul 22 '05 #3
chetan wrote:
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-lit...o-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

Jul 22 '05 #4
Thomas Matthews wrote:
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.
Jul 22 '05 #5
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
Jul 22 '05 #6
On Tue, 02 Nov 2004 08:48:15 GMT, JKop <NU**@NULL.NULL> wrote:
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
}


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

--
Bob Hairgrove
No**********@Home.com
Jul 22 '05 #7
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

David
chetan wrote:
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 ?

Jul 22 '05 #8
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);

Jul 22 '05 #9


JKop wrote:
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);


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
Jul 22 '05 #10
David Lindauer posted:


JKop wrote:
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);


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
Jul 22 '05 #11


JKop wrote:
David Lindauer posted:


JKop wrote:
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);


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


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
Jul 22 '05 #12

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

Similar topics

3
by: Markus Dehmann | last post by:
I have a class "Data" and I store Data pointers in an STL set. But I have millions of inserts and many more lookups, and my profiler found that they cost a lot of runtime. Therefore, I want to...
89
by: Sweety | last post by:
hi, Is main function address is 657. its show in all compiler. try it & say why? bye,
3
by: Dennis Chang | last post by:
Hi all, I was reading about function pointers and came across something which intrigued me. K&R2 calls qsort (pg.119) within main as so: qsort( (void **) lineptr, 0, nlines-1, (int (*) (void...
41
by: Alexei A. Frounze | last post by:
Seems like, to make sure that a pointer doesn't point to an object/function, NULL (or simply 0) is good enough for both kind of pointers, data pointers and function pointers as per 6.3.2.3: 3 An...
12
by: Bill Pursell | last post by:
The following code generates a compiler warning when compiled with gcc -pedantic: typedef (*FUNC)(int); FUNC f; void * get_f(void) { return &f;
57
by: Robert Seacord | last post by:
i am trying to print the address of a function without getting a compiler warning (i am compiling with gcc with alot of flags). if i try this: printf("%p", f); i get: warning: format %p...
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); }
4
by: Christian Maier | last post by:
Hi After surfing a while I have still trouble with this array thing. I have the following function and recive a Segmentation fault, how must I code this right?? Thanks Christian Maier
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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...

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.