• Advertisement
Sign in to follow this  

PHP Style function calling in C++

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

Hello fellas, i have a question.

In PHP you can do something like this

function thisIsAFunction() {

}

$functionName = "thisIsAFunction";

$functionName();




Is there a way to do something simliar?

I'm asking this because i'm creating a timer class for my game. I want to be able to create a timer, and pass along the name of the function to call from the object (which i also pass when creating the function).
I thought about passing the function just as it is. But this causes some trouble. I would have to know the class of the object im passing first, which i just can't figure out.

Edit
I found the preprocessor "stringify" character: #. It converts a variable into a string, or something like that. Is this something i can use? Tried putting it into a macro, but got: ("#" may not appear in macro parameter list) thrown back at me. Am i completly wrong here?

Share this post


Link to post
Share on other sites
Advertisement
In c/c++, they're called function pointers. But once you get the idea behind them, you will probably want to use boost:: or std::function, depending on your compiler.

Share this post


Link to post
Share on other sites
Functors are really easy to use

class Functor
{
public:
virtual ~Functor(void){}
virtual void call(void)
{

}
};

class SFunctor : public Functor
{
public:
typedef void (*func)(void);
SFunctor(func f)
{
Function = f;
}
void call(void)
{
Function();
}
func Function;
};

template<class T>
class SFunctorMethod : public Functor
{
public:
typedef void (T::*func)(void);
SFunctorMethod(T* obj, func f)
{
Object = obj;
Function = f;
}
void call(void)
{
(Object->*Function)();
}
T* Object;
func Function;
};
#include <iostream>
void f(void)
{
std::cout<<"Simple Function"<<std::endl;
}
class test
{
public:
test(){}
void memberMethod(void)
{
std::cout<<"Class method"<<std::endl;
}
};
int main(int argc, char* args[])
{
test instance;
Functor* f1 = new SFunctor(f);
Functor* f2 = new SFunctorMethod<test>(&instance, &test::memberMethod);

f1->call();
f2->call();
return 0;
}

Share this post


Link to post
Share on other sites
I know about function pointers :) But that was my problem. I have to know what type of class the function belongs to. When passing member functions you have to define the type differently then when just passing normal functions.

E.g


class A {
test();
};

obj *A;
obj = new A();

void anotherTest();

...

/* For the normal function */
void normalFunction((void *)());
normalFunction(anotherTest);

/* For the member function */
void memberFunction(A::*)());
memberFunction(obj->test);





As you see, i have to know that the function belongs to the class A, and i don't. I have a list of all objects in my game, and they derrive from a base object. But this base object does not contain all the functions that the derrived classes have. You see my problem now? :)

Edit after reading Sudi's post
Ahh! I spy with my little eye that you're using tempaltes. Could this be what i want? If i use templates, i don't have to know the class type when defining the function?

Share this post


Link to post
Share on other sites
Quote:
Original post by theOcelot
In c/c++, they're called function pointers. But once you get the idea behind them, you will probably want to use boost:: or std::function, depending on your compiler.


QFE. No need to reinvent the wheel here...

Share this post


Link to post
Share on other sites
Quote:
Original post by Zomgbie
I know about function pointers :) But that was my problem. I have to know what type of class the function belongs to. When passing member functions you have to define the type differently then when just passing normal functions.

E.g
*** Source Snippet Removed ***

As you see, i have to know that the function belongs to the class A, and i don't. I have a list of all objects in my game, and they derrive from a base object. But this base object does not contain all the functions that the derrived classes have. You see my problem now? :)


I'd recomend using boost/std::fuction with help from boost/std::bind to be able to hook up a member function from a particular instance of a class.

Share this post


Link to post
Share on other sites
Thank you all for your help! And especially you Sudi, that example was great. Finnaly i understand how to work with templates :)

Share this post


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

  • Advertisement