By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,016 Members | 2,265 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,016 IT Pros & Developers. It's quick & easy.

declaring a function pointer variable

P: n/a
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)
{

}


Jul 23 '05 #1
Share this Question
Share on Google+
2 Replies


P: n/a

"lou zion" <il********@adelphia.net> wrote in message
news:E8********************@adelphia.com...
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


Jul 23 '05 #2

P: n/a
lou zion wrote:

"lou zion" <il********@adelphia.net> wrote in message
news:5O********************@adelphia.com...

"Rolf Magnus" <ra******@t-online.de> wrote in message
news:cv*************@news.t-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?

Jul 23 '05 #3

This discussion thread is closed

Replies have been disabled for this discussion.