tags:

views:

155

answers:

3

Hello, i received a small problem to resolve in my free time regarding changing an objects behavior and also the class behavior at runtime in C++. I read a little bit and found this link, really useful for me. http://www.vollmann.ch/en/pubs/meta/meta/meta.html Mr. Volmann made a Meta object protocol(MOP) for defining classes at runtime, more exactly their properties.

I tried to add methods in the same matter, at runtime, with dynamically loading dlls but the objects weren't aware about their new added behaviors. i saved the details about the methods in the dlls in xml files, and when i wanted to add a new behavior to a class/object i parsed the xml files and loaded only the DLL i needed, but this wasn't flexible enough..

this is just a study case, so if you have any guidance to give please do. I don't have much experience in C++ but i liked the challenge.

Thank you in advance.

+1  A: 

You won't be able to add Methods to a C++ object via dynamic library loading. It just doesn't work that way.

Generally, people store whole objects in DLL's/so's... And even that is a pain with C++ (due to name mangling).. The idiom you'll see again and again is a single function declared in an extern "C" block that you locate (via dlsym() or whatever) that returns a factory class that can be used to create a range of different objects.

dicroce
+2  A: 

You could try to create some kind of base class for all of your classes. This class has some kind of add method for function pointers. Each fp is assigned some kind of handle or string. Later you can then call the added function via some kind of generic call method.

for the arguments of the function - you'll need some way to pass this to the function for referencing the data of the class. Perhaps you'll encapsule the data in some kind of struct and pass a reference/pointer to the struct. For other arguments, you could use some list of pointers or take a look at bind.

Be aware - this is a major undertaking as C++ is not created with these kind of things in mind.

Tobias Langner
+2  A: 

Tobias L's method above is the only solution I see as possible, the real difficulty comes (as TL says) in passing and binding the arguments to the new methods - it seems to me that most of the very technical code might be helped a long a lot by by using standard boost library functionality. Specifically boost::bind and function/functional stuff seems to provide a lot of what you might need to make 'methods' following the framework of Mr Volmann's 'attributes'.

Elemental