Is it possible to create a struct type based on file contents?
Is it possible in C++ to create a struct type based on file contents?
STRUCT_NAME int var1; int var2; string v3; STRUCT NAM开发者_运维问答E *pointer;
The first line would be the name of the struct, the other lines would be the variables
Do you mean on the fly, when your application is running? No, you can't do that, all types must be known at compile time. Of course, you can always create some kind of container object that could be configured dynamically during run-time, but that is a much more advanced system.
On the other hand, if you mean to do this once when you build you application, you could write a simple tool that eats your text file and emits a C++ header file, that you later could use when compiling.
If you are reading from a file and trying to create this struct
,
--> if in same program, i.e. runtime then NO; because you can not compile the source code which is presently executing
--> for some different source code which is yet to be compiled and you are manipulating using file operations then YES
No. The closest you can come is an std::map<std::string, boost::any>
(or boost::variant
if you can limit the set of types).
Whatever @lindydancer @iammilind @james are saying is correct.. You cannot do it in single go. You may get work around (Though thats not professinal way of doing) like this :
1. First read the file in which your structure is define and create new file(Cpp and h files).
2. Now compile new file and provide new exe file to your user, you can do this process in background so that for user its all dynamic.. (But atlease someone has to do this work...)
精彩评论