 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
maths_fan Guest
|
Posted: Fri Feb 27, 2004 2:49 pm Post subject: function as a parameter |
|
|
I know that the correct is when I write:
void g(int);
void f(void*);
.............
f(g);
But my compiler (Visual C++ 6 and .NET) don't mind when I use it like this:
f(void g(int a));
Is it correct by standard of C++ to write like this?
Thanks in advance.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Ben Hutchings Guest
|
Posted: Sat Feb 28, 2004 3:43 am Post subject: Re: function as a parameter |
|
|
maths_fan wrote:
| Quote: | I know that the correct is when I write:
void g(int);
void f(void*);
............
f(g);
|
No, that's not correct. There is no standard conversion from
pointer-to-function to void *. There are good reasons for this,
including the fact that code and data may be stored in separate
address spaces, particularly in micro-controllers.
| Quote: | But my compiler (Visual C++ 6 and .NET) don't mind when I use it like this:
f(void g(int a));
Is it correct by standard of C++ to write like this?
|
No, it's not correct. What you've written there is a declaration
(though of an archaic and non-standard form) rather than an
expression. The parameter names g and a are ignored, the function
type given as a parameter type is "adjusted" to a pointer-to-function
type, and the return type is assumed to be int. So it is equivalent
to:
int f(void (*)(int));
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|