returning a void (*)(void)

S

Sergio

If I have a private member:

void (*func)(void);

how can i declare a 'get' function that returns it? I tryed:

void (*)()GetFunc()
{
return func;
}

but looks like that's not the way...

thanks
 
B

Bob Hairgrove

If I have a private member:

void (*func)(void);

how can i declare a 'get' function that returns it? I tryed:

void (*)()GetFunc()
{
return func;
}

but looks like that's not the way...

thanks

It's easier with a typedef:

typedef void (* func_t)();

struct C {
func_t func;
func_t getFunc() { return func; }
};
 
D

Dietmar Kuehl

Sergio said:
If I have a private member:

void (*func)(void);

Note that this signature matches a member only if the member
is static: otherwise the type is more something like this

| void (C::*func)(void);

where 'C' is the class containing the member.
how can i declare a 'get' function that returns it? I tryed:

void (*)()GetFunc()

| void (*GetFunc())()

is the syntax for a void function returning void.
 
O

owl ling

well, yoiu should writen as the following code.
typedef void (*FUNC)(void);

void Func(void)
{
cout << "func" << endl;
}
FUNC GetFunc()
{
FUNC fp = Func;
return fp;
}
 
M

msalters

Sergio said:
If I have a private member:

void (*func)(void);

how can i declare a 'get' function that returns it? I tryed:

void (*)()GetFunc()
{
return func;
}

but looks like that's not the way...

Don't try it this way, use a typedef.

typedef void(*fun_void_void)();
fun_void_void func;
fun_void_void GetFunc() { return func; }
HTH,
Michiel Salters
 
O

Old Wolf

msalters said:
Don't try it this way, use a typedef.

typedef void(*fun_void_void)();
fun_void_void func;
fun_void_void GetFunc() { return func; }

Another option is to typedef the function type (rather than the
pointer-to-function). I only mention this because it hadn't
occurred to me that it was possible until recently, and I prefer
to avoid pointer typedefs if I can:

| typedef void (fun_void_void) ();
| fun_void_void func; // this declares a function
| fun_void_void *ptr_func = func;
| fun_void_void * GetFunc() { return ptr_func; }

or

| fun_void_void * GetFunc() { return func; }

because the name of a function is converted to a pointer to
that function, in a value context.
 
J

Jonathan Turkanis

Sergio said:
If I have a private member:

void (*func)(void);

how can i declare a 'get' function that returns it? I tryed:

On more way:

#include <boost/mpl/identity.hpp>

using namespace boost::mpl;

identity<void (*)(void)>::type get();
 

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

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top