declaring a function pointer variable

Discussion in 'C++' started by lou zion, Feb 25, 2005.

  1. lou zion

    lou zion Guest

    hi all,

    i've got a class that takes a parameterless function pointer as a parameter.
    i want to store that function pointer in a variable and i'm trying to figure
    out the syntax. i came up with the stuff below, but it's telling me it can't
    convert from void *(void) to void(void), which i didn't think i was doing.
    what's the proper syntax for defining and initializing a function pointer
    like this?

    thnks a bunch,

    lou

    class TabWatcher : public QObject
    {
    public:
    TabWatcher( const char * name = 0, QWidget * NewFocusWidget = 0, void
    *funcptr()=0 ) ;
    ~TabWatcher();

    protected:
    void (*FuncPtr)();

    }


    TabWatcher::TabWatcher( const char * name, QWidget * NewFocusWidget, void
    *funcptr()) : QObject(parent,0),
    newfocus(NewFocusWidget),
    FuncPtr(funcptr)
    {

    }
     
    lou zion, Feb 25, 2005
    #1
    1. Advertising

  2. lou zion

    Howard Guest

    "lou zion" <> wrote in message
    news:...
    > hi all,
    >
    > i've got a class that takes a parameterless function pointer as a
    > parameter. i want to store that function pointer in a variable and i'm
    > trying to figure out the syntax. i came up with the stuff below, but it's
    > telling me it can't convert from void *(void) to void(void), which i
    > didn't think i was doing. what's the proper syntax for defining and
    > initializing a function pointer like this?
    >
    > thnks a bunch,
    >
    > lou
    >
    > class TabWatcher : public QObject
    > {
    > public:
    > TabWatcher( const char * name = 0, QWidget * NewFocusWidget = 0, void
    > *funcptr()=0 ) ;
    > ~TabWatcher();
    >
    > protected:
    > void (*FuncPtr)();
    >
    > }
    >
    >
    > TabWatcher::TabWatcher( const char * name, QWidget * NewFocusWidget, void
    > *funcptr()) : QObject(parent,0),
    > newfocus(NewFocusWidget),
    > FuncPtr(funcptr)
    > {
    >
    > }
    >


    I find it quite useful to define a type for my function pointers. It makes
    constructing the declarations much easier. For example,

    typedef void (*VOIDFUNC_TYPE)();
    ....
    void bar() {...}
    ....
    void foo( VOIDFUNC_TYPE funptr ) {...}
    ....
    foo(bar);


    -Howard
     
    Howard, Feb 25, 2005
    #2
    1. Advertising

  3. lou zion

    Rolf Magnus Guest

    lou zion wrote:

    > hi all,
    >
    > i've got a class that takes a parameterless function pointer as a
    > parameter. i want to store that function pointer in a variable and i'm
    > trying to figure out the syntax. i came up with the stuff below, but it's
    > telling me it can't convert from void *(void) to void(void), which i
    > didn't think i was doing. what's the proper syntax for defining and
    > initializing a function pointer like this?


    You defined it correctly as a member variable., but the parameter looks
    different. Why?

    >
    > thnks a bunch,
    >
    > lou
    >
    > class TabWatcher : public QObject
    > {
    > public:
    > TabWatcher( const char * name = 0, QWidget * NewFocusWidget = 0, void
    > *funcptr()=0 ) ;


    TabWatcher( const char * name = 0, QWidget * NewFocusWidget = 0,
    void (*funcptr)() = 0);

    > ~TabWatcher();
    >
    > protected:
    > void (*FuncPtr)();
    >
    > }

    ;
     
    Rolf Magnus, Feb 26, 2005
    #3
  4. lou zion

    lou zion Guest


    > I find it quite useful to define a type for my function pointers. It
    > makes
    > constructing the declarations much easier. For example,
    >
    > typedef void (*VOIDFUNC_TYPE)();
    > ...
    > void bar() {...}
    > ...
    > void foo( VOIDFUNC_TYPE funptr ) {...}
    > ...
    > foo(bar);



    i like this approach, so i gave it a whirl.

    class TabWatcher
    {
    public:
    TabWatcher(const char * name = 0, QWidget * NewFocusWidget = 0,
    VOIDFUNC_TYPE funcptr=0 ) ;
    ~TabWatcher();

    protected:
    QWidget *newfocus;
    VOIDFUNC_TYPE FuncPtr;
    }

    TabWatcher::TabWatcher(const char * name, QWidget * NewFocusWidget,
    VOIDFUNC_TYPE funcptr) :
    newfocus(NewFocusWidget), FuncPtr(funcptr)
    {
    }

    this compiles and runs fine so long as i don't make a tabwatcher class
    adding an actual function pointer. in another class 'mainclass' i have a
    function:

    void mainclass::passfunc()
    {
    }

    again, compiles and runs fine. in my constructor of mainclass i call:
    TabWatcher *newptr = new TabWatcher( 0, rbHomePriceLocked, passfunc );

    this doesn't compile, spitting up the error:
    cannot convert parameter from 'void (void)' to 'VOIDFUNC_TYPE'

    if i try casting it like this:
    TabWatcher *newptr = new TabWatcher( 0, rbHomePriceLocked, (VOIDFUNC_TYPE)
    passfunc );

    i then get the error:
    cannot convert from 'overloaded-function' to 'VOIDFUNC_TYPE'

    well, the function is not overloaded. what can i try now?

    thnx again!
    lou
     
    lou zion, Feb 26, 2005
    #4
  5. lou zion

    lou zion Guest

    "Rolf Magnus" <> wrote in message
    news:cvofv9$l63$02$-online.com...

    >> i've got a class that takes a parameterless function pointer as a
    >> parameter. i want to store that function pointer in a variable and i'm
    >> trying to figure out the syntax. i came up with the stuff below, but it's
    >> telling me it can't convert from void *(void) to void(void), which i
    >> didn't think i was doing. what's the proper syntax for defining and
    >> initializing a function pointer like this?

    >
    > You defined it correctly as a member variable., but the parameter looks
    > different. Why?
    >


    i tried several ways of doing it and nothing was compatible when i actually
    tried to invoke a call to a function with a function-pointer parameter. that
    never seems to compile. so, i played around.

    i can get it to compile if i declare the parameter exactly like the variable
    i.e. void (*func)(), but i still can't seem to get it to compile when i try
    to use it in a call. i always get this:
    cannot convert parameter 3 from 'void (void)' to 'void (__cdecl *)(void)'

    using vc++ .net (without any .net). i don't believe this is a vc++ thing,
    though.
    i'm still lost. i don't know if it makes any difference, but i'm using Qt.

    thanks for any help.

    lou
     
    lou zion, Feb 26, 2005
    #5
  6. lou zion

    lou zion Guest

    "lou zion" <> wrote in message
    news:...
    >
    > "Rolf Magnus" <> wrote in message
    > news:cvofv9$l63$02$-online.com...
    >
    >>> i've got a class that takes a parameterless function pointer as a
    >>> parameter. i want to store that function pointer in a variable and i'm
    >>> trying to figure out the syntax. i came up with the stuff below, but
    >>> it's
    >>> telling me it can't convert from void *(void) to void(void), which i
    >>> didn't think i was doing. what's the proper syntax for defining and
    >>> initializing a function pointer like this?

    >>
    >> You defined it correctly as a member variable., but the parameter looks
    >> different. Why?
    >>

    >
    > i tried several ways of doing it and nothing was compatible when i
    > actually tried to invoke a call to a function with a function-pointer
    > parameter. that never seems to compile. so, i played around.
    >
    > i can get it to compile if i declare the parameter exactly like the
    > variable i.e. void (*func)(), but i still can't seem to get it to compile
    > when i try to use it in a call. i always get this:
    > cannot convert parameter 3 from 'void (void)' to 'void (__cdecl *)(void)'
    >
    > using vc++ .net (without any .net). i don't believe this is a vc++ thing,
    > though.
    > i'm still lost. i don't know if it makes any difference, but i'm using Qt.
    >
    > thanks for any help.
    >
    > lou


    another twist on this. if i declare a global function and pass it, it
    compiles fine. there's something about passing a class function that's
    messing it up.

    lou
     
    lou zion, Feb 26, 2005
    #6
  7. lou zion wrote:
    > hi all,
    >
    > i've got a class that takes a parameterless function pointer as a parameter.
    > i want to store that function pointer in a variable and i'm trying to figure
    > out the syntax. i came up with the stuff below, but it's telling me it can't
    > convert from void *(void) to void(void), which i didn't think i was doing.
    > what's the proper syntax for defining and initializing a function pointer
    > like this?


    I've tried to do the same thing, and decided that it's a lot easier to
    pass fuctors (objects with the call operator overloaded) around, rather
    than proper functions.

    I other words, define a class like this (untested):

    class Func
    {
    public:
    void operator() () const;
    }

    void Func::eek:perator() () const
    {
    // do some stuff here
    }

    Now, you can pass objects of type Func around like objects, and call
    them like functions. This shouldn't cause any significant performance
    hit, and is much easier to code.

    Rennie
     
    Rennie deGraaf, Feb 26, 2005
    #7
  8. lou zion

    Rolf Magnus Guest

    lou zion wrote:

    >
    > "lou zion" <> wrote in message
    > news:...
    >>
    >> "Rolf Magnus" <> wrote in message
    >> news:cvofv9$l63$02$-online.com...
    >>
    >>>> i've got a class that takes a parameterless function pointer as a
    >>>> parameter. i want to store that function pointer in a variable and i'm
    >>>> trying to figure out the syntax. i came up with the stuff below, but
    >>>> it's
    >>>> telling me it can't convert from void *(void) to void(void), which i
    >>>> didn't think i was doing. what's the proper syntax for defining and
    >>>> initializing a function pointer like this?
    >>>
    >>> You defined it correctly as a member variable., but the parameter looks
    >>> different. Why?
    >>>

    >>
    >> i tried several ways of doing it and nothing was compatible when i
    >> actually tried to invoke a call to a function with a function-pointer
    >> parameter. that never seems to compile. so, i played around.
    >>
    >> i can get it to compile if i declare the parameter exactly like the
    >> variable i.e. void (*func)(), but i still can't seem to get it to compile
    >> when i try to use it in a call. i always get this:
    >> cannot convert parameter 3 from 'void (void)' to 'void (__cdecl *)(void)'
    >>
    >> using vc++ .net (without any .net). i don't believe this is a vc++ thing,
    >> though.
    >> i'm still lost. i don't know if it makes any difference, but i'm using
    >> Qt.
    >>
    >> thanks for any help.
    >>
    >> lou

    >
    > another twist on this. if i declare a global function and pass it, it
    > compiles fine. there's something about passing a class function that's
    > messing it up.


    You mean a non-static member function? Those are different from other
    functions, because they need an object they are called for, even when
    called through a pointer. Therefore, a pointer a to non-static member
    function is not compatible to a regular function pointer. An example of how
    to use them:

    #include <iostream>

    struct Foo
    {
    void print(int x)
    {
    std::cout << "The value is "
    << (i == x ? "equal to " : "not equal to ")
    << x << '\n';
    }
    int i;
    };

    int main()
    {
    Foo obj = { 42 };

    void (Foo::* func)(int) = &Foo::print;
    (obj.*func)(10);
    }

    Btw: what do you need the function pointers for?
     
    Rolf Magnus, Feb 28, 2005
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brandon
    Replies:
    2
    Views:
    492
    Arne Adams
    Aug 8, 2004
  2. iceColdFire
    Replies:
    9
    Views:
    525
    John Carson
    May 23, 2005
  3. Replies:
    12
    Views:
    1,055
    Keith Thompson
    Sep 17, 2005
  4. vlsidesign
    Replies:
    2
    Views:
    242
    Barry
    Jan 11, 2007
  5. pembed2012
    Replies:
    1
    Views:
    455
    James Kuyper
    Feb 27, 2012
Loading...

Share This Page