Connecting Tech Pros Worldwide Forums | Help | Site Map

declaring a function pointer variable

lou zion
Guest
 
Posts: n/a
#1: Jul 23 '05
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)
{

}





Howard
Guest
 
Posts: n/a
#2: Jul 23 '05

re: declaring a function pointer variable



"lou zion" <illuzioner@adelphia.net> wrote in message
news:E8idnSUoy9yyOoLfRVn-oQ@adelphia.com...[color=blue]
> 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)
> {
>
> }
>[/color]

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




Rolf Magnus
Guest
 
Posts: n/a
#3: Jul 23 '05

re: declaring a function pointer variable


lou zion wrote:
[color=blue]
>
> "lou zion" <illuzioner@adelphia.net> wrote in message
> news:5OudnaVf4tQrVoLfRVn-jA@adelphia.com...[color=green]
>>
>> "Rolf Magnus" <ramagnus@t-online.de> wrote in message
>> news:cvofv9$l63$02$2@news.t-online.com...
>>[color=darkred]
>>>> 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?
>>>[/color]
>>
>> 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[/color]
>
> 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.[/color]

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?

Closed Thread