Jump to content
  • Advertisement
Sign in to follow this  
jamesleighe

Function that returns a function pointer

This topic is 2543 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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

Share this post


Link to post
Share on other sites
Advertisement
You could do:


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

but you should probably just typedef the function pointer.

Share this post


Link to post
Share on other sites
Hidden
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

Share this post


Link to post
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

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!