Jump to content
  • Advertisement

Archived

This topic is now archived and is closed to further replies.

bilsa

templated function pointer?

This topic is 5413 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

Is there any way of making a templated function pointer? something like this:
//This is the templated function to point to:

template <class T>
void testFunc(T* unknownObj) {
//...

}
//this is the "templated function pointer":

template <class T>
void (*pFn)(T*) = Create;
Is there any way of achieving what I want? I don''t want to instantiate for every T i want to have a functio pointer to...? the compiler says that the "''function pointer'' cannot be templated" This is C++ VC++ 6.0 C ya all!

Share this post


Link to post
Share on other sites
Advertisement
This is somewhat the effect I'm looking for:


//the templated function to point to

template <class T>
void testFunc(void* unknownObj) {
//now... you want your defined object right?

T* desiredObj = reinterpret_cast<T*>(unknownObj);
}
//And the function pointer:

typedef void (*pFn)(void*);
//now you can use the function pointer for any instance of testFunc(void* unknownObj)

pFn pFuncWithSomeClass = testFunc<SomeClass>;
pFn pFuncString = testFunc<char>;
pFn pFuncSomeOtherClass = terstFunc<SomeOtherClass>;



But I would say it's just a "hack"/workaround for what I wanted. If someone else know how to do it withouth this "hack", I would appreciate it.

The "hack" also works for this:


template <class T>
void* Create(char* name) {
return new T;
}
//now I need a templated function pointer! right?

//But I don't know how to do it?!

//The same "hack" above works for this situation:

template <class T>
void* Create(char* name) {
return new T;
}
typedef void* (*pFn)(char*);
pFn pFuncSomeClass = Create<SomeClass>;
//I would need to typecast the returned pointer :(

SomeClass* tempObj = reinterpret_cast<SomeClass*>((*pFuncSomeClass)("SomeValue"));
//this "hack" only works since I KNOW what type the

//function returns... since I need to make a cast.

//BUT WHAT IF I WOULDN'T KNOW?




But as I said I would rather want a clean way with a templated function pointer, if anyone knows how to do it. thx!

[edited by - bilsa on January 18, 2004 11:12:31 AM]

[edited by - bilsa on January 18, 2004 11:30:53 AM]

[edited by - bilsa on January 18, 2004 11:32:04 AM]

[edited by - bilsa on January 18, 2004 11:43:51 AM]

[edited by - bilsa on January 18, 2004 11:44:44 AM]

Share this post


Link to post
Share on other sites
Is this the kind of thing you''re looking for?
#include <iostream>

template <typename T>
class pFn
{

public:

typedef void (*PFN)(T*);

pFn(PFN p)
{
func = p;
}

void operator()(T* t)
{
func(t);
}

private:

PFN func;

};

template <typename T>
void testFunc(T* t)
{
std::cout << *t << ''\n'';
}

int main()
{
pFn<int> intPFn(testFunc);
pFn<float> floatPFn(testFunc);
pFn<char*> charPointerPFn(testFunc);
int i = 7;
float f = 32.2f;
char* cp = "test string";
intPFn(&i);
floatPFn(&f);
charPointerPFn(&cp);
}


I''d suggest you have a good look at your design though - there''s probably a far easier and better way to do what you want.

Enigma

Share this post


Link to post
Share on other sites
Nah, that''s not entirely what I wanted... what you did is something similar to a functor, shich I already have implemented.

I actually need this for a Object factory.
It works something like this:


//the object factory has this:

//...

typedef void* (*pFnInterfaceInstance)();
std::map<const char*, pFnInterfaceInstance> CreateCallbackMap;
RegisterInterface(const char* sVersion, pFnInterfaceInstance GetInstance);
//...


//this is how the object registers itself...

//...

namespace {
void* GetISoundBaseInterface() {
return new ISoundBase;
}

Interfaces::RegisterInterface("BaseInterface", GetISoundInterface);
}
//...

//then I have a newer version of the ISoundInterface

namespace {
void* GetISoundV2Interface() {
return new ISoundV2;
}

Interfaces::RegisterInterface("v.2", GetISoundV2Interface);
}
//...

//Lets say I have a Dll that Implements the Game stuff... and

//lets say I need to be able to use the Implementations of my

//ISound interface for playing sound.... so I have something

//like this:

class Game {
//...

ISoundBase* soundInterface;
//...

};

//then I would load animplementetion of the sound interface

//and I could choose to use an older version of it if I

//would want... but the standard would be the latest version

//of the interface...

//etc...


Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

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

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!