Function that returns a function pointer

Started by
1 comment, last by Misery 12 years, 3 months ago
how do you implement a function that returns a function pointer without using typedefs that's also in a class? (in c++)

i'm pretty sure i can declare it like this

class Stupid
{
public:
void (*GetFnValue( char* key ))( );
};


but i'm lost at implementation, something like

void (*fn)( )
Stupid::GetFnValue( char* key )
{
return whatev;
}


tanks
Advertisement
You could do:


void (*Stupid::GetFnValue( char* key ))()
{
return 0;
}

but you should probably just typedef the function pointer.
If it is C++ maybe it is worth trying to use functor instead of function pointers.

class Stupid
{
public:
void (*GetFnValue( char* key ))( );
};


The above code could be changed to:

class Stupid
{
public:
MyFunctorClass f;
};


and the latter part:

class MyFunctor{
public:
MyFunctor(char* key)
{
//do some stuff
}
}


And then you can manipulate your functions just like variables:

MyFunctorClass fun;
Stupid s;
s.f=fun
//use the function
s.f(argument)

and if You want a function returning a function it would just be sth like this:

MyFunctorClass Stupid::FunRetFun(argtypes arguments)
{
//do something
return SomeInstance of MyFunctorClass;
}


I don't know if thats handy in Your application, but in a few programs that I have created it worked very well and it was much more comfy to use.
Regards
If it is C++ maybe it is worth trying to use functor instead of function pointers.

class Stupid
{
public:
void (*GetFnValue( char* key ))( );
};


The above code could be changed to:

class Stupid
{
public:
MyFunctorClass FunRetFun(argtypes arguments)
};


and the latter part:

class MyFunctor{
public:
MyFunctor(char* key)
{
//do some stuff
}
}



and if You want a function returning a function it would just be sth like this:

MyFunctorClass Stupid::FunRetFun(argtypes arguments)
{
//do something
return SomeInstance of MyFunctorClass;
}


I don't know if thats handy in Your application, but in a few programs that I have created it worked very well and it was much more comfy to use.
Regards
Oh my... deleting or editing posts do not work. :(

This topic is closed to new replies.

Advertisement