Creating array/object at runtime.
-
Hi. I want to know how I can create an array/object at runtime if I don’t know about its data type. For example, I have an array Say Arr, but I don’t have any information about the data type of elements it stores. It might be an array of int, float, char or even user defined data type (user defined class). I can get the information about its class or data type by using ‘type_info’, but how can I create an array/object of same data type at runtime? Like if I want to copy the contents of an array Arr into another array say ‘NewArr’; of the same type at runtime, how can I create that second array NewArr, i.e. what should I specify as a data type of ‘NewArr’?
Thanks Sameer Thakur
-
Hi. I want to know how I can create an array/object at runtime if I don’t know about its data type. For example, I have an array Say Arr, but I don’t have any information about the data type of elements it stores. It might be an array of int, float, char or even user defined data type (user defined class). I can get the information about its class or data type by using ‘type_info’, but how can I create an array/object of same data type at runtime? Like if I want to copy the contents of an array Arr into another array say ‘NewArr’; of the same type at runtime, how can I create that second array NewArr, i.e. what should I specify as a data type of ‘NewArr’?
Thanks Sameer Thakur
Sounds like a job for a
template
.
"A good athlete is the result of a good and worthy opponent." - David Crow
"To have a respect for ourselves guides our morals; to have deference for others governs our manners." - Laurence Sterne
-
Hi. I want to know how I can create an array/object at runtime if I don’t know about its data type. For example, I have an array Say Arr, but I don’t have any information about the data type of elements it stores. It might be an array of int, float, char or even user defined data type (user defined class). I can get the information about its class or data type by using ‘type_info’, but how can I create an array/object of same data type at runtime? Like if I want to copy the contents of an array Arr into another array say ‘NewArr’; of the same type at runtime, how can I create that second array NewArr, i.e. what should I specify as a data type of ‘NewArr’?
Thanks Sameer Thakur
Sameer_Thakur wrote:
how can I create an array/object of same data type at runtime?
AFAIK, there's no way to do this directly in C++. You can, however, design a system of classes that are capable of creating/cloning themselves at runtime and/or being created by type. For an example implementation, check out the source code for MFC's CRuntimeClass and CObject classes and the DECLARE_DYNAMIC/IMPLEMENT_DYNAMIC macros. This implementation has a common base class which includes methods for runtime object creation based on a class name. Because C++ has no built-in way to do this, the CRuntimeClass implements a list that maps class names to creator functions. Depending on the source of your source arrays, serialization may be useful as well. Regardless, at the heart of these solutions is code that creates objects of a specific class, which is all that C++ allows. Just my 2-cents, Mark
"Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder