This is not I want. I want other one can add their class name in the classlist.txt and implement their drive() function. I don't know the class name is mycar or yourcar. I only need to call the drive() of that class. I found in the C# reflection can implement this, I don't know if C++ can implement this.
I need implement the main() that don't care about the class name, but can call the class function drive() in C++. Is that possible?
No, I don't think this can be done in C++ since a class is a compile time object.
C# and Java have reflection alowing one to inject classes into the runtime environment. This can be cool, but there are security issues that have to be addressed. C# does a stack walk and signing I think and Java does signing and a sandbox.
C++ doesn't use those concepts, so it would be very risky to do. To do what you want would require:
- a compiler if you want source to run using a native binary
- a dll if you want to give the user the ability to extend the code with a binary (no source, or you can mix with point 1).
- an interpreter if you don't mind interpreting the code, either implement one yourself (good luck ;)) or get one premade (C-- comes to mind)
- a rudamentary parser if you want to make a framework that will make objects that can 'run' the 'code' (not necesarly C++ code)
NOTE 1 & 2 are also somewhat of a security risk too. Someone can crash your code by doing those.
One other thing about C#, how do you read in a file and invoke the object's method if you don't know the name of the object's type?
EDIT: IF you want to do this at compile time, you could create an interface using a virtual function (this is called interface inheirtance or a template pattern
which is unrelated to a C++ template). How you would get that object to your main code thread without knowing the name of the type, I cannot answer.
Adrian