C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

declaring a function pointer variable

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
lou zion
Guest





PostPosted: Fri Feb 25, 2005 10:31 pm    Post subject: declaring a function pointer variable Reply with quote



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

}




Back to top
Howard
Guest





PostPosted: Fri Feb 25, 2005 11:06 pm    Post subject: Re: declaring a function pointer variable Reply with quote




"lou zion" <illuzioner (AT) adelphia (DOT) net> wrote

Quote:
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





Back to top
Rolf Magnus
Guest





PostPosted: Sat Feb 26, 2005 12:35 am    Post subject: Re: declaring a function pointer variable Reply with quote



lou zion wrote:

Quote:
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?

Quote:

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

Quote:
~TabWatcher();

protected:
void (*FuncPtr)();

}
;



Back to top
lou zion
Guest





PostPosted: Sat Feb 26, 2005 12:38 am    Post subject: Re: declaring a function pointer variable Reply with quote


Quote:
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



Back to top
lou zion
Guest





PostPosted: Sat Feb 26, 2005 1:07 am    Post subject: Re: declaring a function pointer variable Reply with quote


"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote


Quote:
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



Back to top
lou zion
Guest





PostPosted: Sat Feb 26, 2005 1:28 am    Post subject: Re: declaring a function pointer variable Reply with quote


"lou zion" <illuzioner (AT) adelphia (DOT) net> wrote

Quote:

"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote in message
news:cvofv9$l63$02$2 (AT) news (DOT) 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.

lou




Back to top
Rennie deGraaf
Guest





PostPosted: Sat Feb 26, 2005 1:37 am    Post subject: Re: declaring a function pointer variable Reply with quote

lou zion wrote:
Quote:
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::operator() () 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

Back to top
Rolf Magnus
Guest





PostPosted: Mon Feb 28, 2005 12:12 pm    Post subject: Re: declaring a function pointer variable Reply with quote

lou zion wrote:

Quote:

"lou zion" <illuzioner (AT) adelphia (DOT) net> wrote in message
news:5OudnaVf4tQrVoLfRVn-jA (AT) adelphia (DOT) com...

"Rolf Magnus" <ramagnus (AT) t-online (DOT) de> wrote in message
news:cvofv9$l63$02$2 (AT) news (DOT) 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?


Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.