I'm trying to prototype a "plug and play" class.
Basically, just want to have a generic function I don't have to modify but still want to be able to add new classes via a header file.
My first idea :
Look-Up Table with function pointers on static member of a class.
It might be better if you put your intentions into a nice high-level description. Unless there's something I'm missing, I don't see any benefit to this over simple polymorphism. You might be over-thinking the problem here.
So I'm trying to design an application simulating different kind of units.
As the project is ongoing, i don't even know the number of units I have to simulate neither their functionalities, but I know they will heritate from the same Base Class.
I would like to be able to add/remove a unit at any point in time, by just having a Look-up Table with the right configuration.
Say I want to add a unit, I just add something in this look-up table and the unit will be available to the simulation.
I would like to not modify the main process creating all the units; just having a generic function able to read the LUT and create the right unit Child Class from there.
Hence the static function and function pointers, but might not be the best way..
So, basically you want to create new data types and functions on run-time. I guess it could be done somehow... I'll have to think of it for a while... Say again, what could the functionalities of these types be? Give an example.
That sounds like the Factory Method pattern (AKA Virtual Constructor). You might want to read up about it and maybe check out the Abstract Factory pattern, as well. I think this problem can be solved without writing your own vtable--let the compiler do that.
Hi guys,
Thanks for answers,
you're both right on your understanding : creating a new Class at run-time.
The factory method looks pretty good indeed, but it does mean having a switch somewhere in the creation class who can create the right type.
Just wonder if there's a way to avoid the switch statement.
The reason for that is a bit of a challenge: I design the software but if someone else wants to modify or add a class, then he just :
- add the .hpp and .cpp corresponding to the new class
- add some line somewhere to tell the simulation the new class needs to be created.
- doesn't add any other link between the first and second statements..
It really is to keep it as simple as possible for the future user of the simulation.
Am I wrong on the understanding of the Factory Method (that I've never used, I admit..)?
Galik> Well, i'm afraid it doesn't do what I'd like.
My problem is that I don't know class A or class B before runtime, so can't create instances a or b.
You will at some point need to edit some of your code to adapt to a new class you want to use.
The best idea would be to create a Factory as pointed above. Then that's the only thing you need to edit when inserting new classes.
If you are trying to make it more a plugin solution, use function pointers and load the function with getfunction or whatever you os provides.
Well, I think I now have the answers I needed (what I can do, what I shouldn't do, what I can't do).
I'm very pleased with your help, thanks every one!