vendredi 30 janvier 2015

invoke object c++. Reflection in c++

I am new to C++ and I'm trying to implement Reflection.


I have a Method class, Field class, and Object class.


Say, that I have an Object of Class Male called Daniel. The class Male has a method called increaseAge() which increases Daniel's age by 1. Daniel has an int field - age.


How do I invoke the Method increaseAge() on Daniel.



typedef void(*Func)(Object*);

class Method{
private:
std::string nameMethod;
Func f;

public:
Method(std::string name,Func fun){
nameMethod=name;
f=fun;
};

/*virtual void invoke(Object* const obj);

if Object has method, then invoke method on obj.

else > throw MethodNotFound.
*/
};


#endif /* METHOD_H_ */


and, my main.cpp looks like this:



#include <iostream>
#include <string>
#include "Field.h"
#include "Method.h"

using namespace std;

void increaseAge(Object* obj){
age++;
}


int main(){
Method* m2 = new Method("increaseAge",increaseAge);
Object Daniel = new Male;
m2.invoke(Daniel);

}





Aucun commentaire:

Enregistrer un commentaire