Re: Pointers to Functions

From: Paul (invisiblepaul_at_hotmail.com)
Date: 10/02/04

  • Next message: Mark P: "Re: Destructor problem"
    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 :)


  • Next message: Mark P: "Re: Destructor problem"

    Relevant Pages

    • Re: fields for methods?
      ... but only by a compiler that is allowed to ... struct A {void foo();}; ... int static_instance i = 0; ... Is not possible because foo is the only member of A... ...
      (comp.programming)
    • Re: fields for methods?
      ... void A::foo{ ... but only by a compiler that is allowed to ... int static_instance i = 0; ... it totally breaks the idea of encapsulation, which is the reason a lot ...
      (comp.programming)
    • Re: Virtual Machine implementation problem, Please help me to spot the bug
      ... This is non-standard (Conceivably your compiler allows it ... tmp1 might not be correctly aligned for u32. ... void change_endian{ ... typedef unsigned int u32; ...
      (comp.lang.c)
    • Re: C Questions
      ... No prototype is given for this function, so the compiler cannot ... To prototype a function with no parameters, use void: ... %d takes an int argument, ...
      (comp.lang.c)
    • Re: The_Sage & void main()
      ... shall have a return type of type int but otherwise in all other respects ... > main may have the return type void then main may indeed have the ... If the compiler accepts void main, then you may use void main ... about whether void mainis conforming or not. ...
      (comp.lang.cpp)