PHP Style function calling in C++

Started by
7 comments, last by Zomgbie 13 years, 4 months ago
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?
Omg, zombie! Zomgbie.
Advertisement
use functors
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.
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;}
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?
Omg, zombie! Zomgbie.
Yes templates are what ya want here.
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...
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.
Thank you all for your help! And especially you Sudi, that example was great. Finnaly i understand how to work with templates :)
Omg, zombie! Zomgbie.

This topic is closed to new replies.

Advertisement