Re: Pointers to Functions
From: Paul (invisiblepaul_at_hotmail.com)
Date: 10/02/04
- Previous message: Paladiamors: "Source code below"
- In reply to: Francis Glassborow: "Re: Pointers to Functions"
- Next in thread: Francis Glassborow: "Re: Pointers to Functions"
- Reply: Francis Glassborow: "Re: Pointers to Functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: 1 Oct 2004 17:19:49 -0700
Francis Glassborow <francis@robinton.demon.co.uk> wrote in message news:<LbbtoVVcSYXBFwGf@robinton.demon.co.uk>...
> In article <1f2809cc.0410010520.2f580dce@posting.google.com>, Paul
> <invisiblepaul@hotmail.com> writes
> >eh?
> >Not according to my compiler.
> >With the code below...
> >
> >#include <iostream>
> >
> >typedef void(*FP)(int);
> >
> >inline void foo(int arg){
> > std::cout<< arg << std::endl;
> >}
> >
> >int main(){
> > FP fp = foo;
> > foo(5);
> >return 0;
> >}
> >
> >Remove the * from the typedef and I get:
> >error C2072: 'fp' initialisation of a function.
>
> That looks like a VC++ error message. G++ in strict pedantic mode is
> perfectly happy, even when I replace your foo(5) with fp(5). And Comeau
> happily compiles it (but with a warning for an unused variable for your
> version)
Yes it is using the latest VC++ optimising compiler, and I believe
this compiler is correct.
If you had the following code:
void foo(){}
int main(){
void (*fp)() = foo;
}
This is fine as the identifier foo is a const pointer to the function
type in much the same way as an arrays name is a pointer to it's 1st
element.
But if you remove the asterix from the function pointer declaration
you'd be aswell removing the brackets too as
void (fp)() = foo;
is the same as
void fp() = foo;
Is it not?
But then this would be a function declaration and not a function
pointer.
So if you typedefed that as
typedef void ft();
then to use it you 'd need to do
ft* = foo;
Which as expected works fine on my compiler.
however
ft =foo;
informs me that I am generating an error by trying to initialise a
function, which is also expected.
Everything seems to work and behave as it should yet you are saying
this is non standard.
You can't assign a function pointer to a function in much the same way
as you can't assign an int* to an int. I don't know where your getting
this from and can only assume to have had some kind of mental lapse.
Paul :)
- Previous message: Paladiamors: "Source code below"
- In reply to: Francis Glassborow: "Re: Pointers to Functions"
- Next in thread: Francis Glassborow: "Re: Pointers to Functions"
- Reply: Francis Glassborow: "Re: Pointers to Functions"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|