declaring a function pointer variable

L

lou zion

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)
{

}
 
H

Howard

lou zion said:
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
 
R

Rolf Magnus

lou said:
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)();

}
;
 
L

lou zion

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
 
L

lou zion

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
 
L

lou zion

lou zion said:
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
 
R

Rennie deGraaf

lou said:
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
 
R

Rolf Magnus

lou said:
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?
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,013
Latest member
KatriceSwa

Latest Threads

Top