Answer the question
In order to leave comments, you need to log in
C++. Create a dynamic array that stores data types as array elements. Is it possible in any way?
We need a mechanism to create a dictionary for the program based on a two-dimensional dynamic array, where <Data type name (string), data type>. In place of "data type" can be a pointer to a function that creates this data type, the address of a prototype object, or anything that allows you to get the desired data type when accessing an array element.
All this is needed for the mechanism of translating the code transmitted by lines during the execution of the program, and this code must be able to call members of the classes existing in the program. I would like to avoid creating huge functions, which will describe all types of data and the mechanism for processing them.
It is preferable to make, for example, a universal global TypeCollector class, which will allow you to simply add the data type of the created class to the TypeCollector in the constructor of any new class, and then create an object of the stored data type from another point in the code through the appropriate method of the TypeCollector class.
Answer the question
In order to leave comments, you need to log in
class IObject
{
public:
virtual void create(void** ret_val) = 0;
virtual std::string name() const = 0;
};
template <typename _Type>
class ObjectT : public IObject
{
protected:
static std::string m_name;
public:
void create(void** ret_val)
{
*ret_val = (void*) new _Type;
}
};
typedef ObjectT<bool> BoolObject;
typedef ObjectT<int> IntObject;
template<> std::string BoolObject::m_name = "bool";
template<> std::string IntObject::m_name = "int";
BoolObject bobj;
IntObject iobj;
std::vector<IObject*> my_vector;
my_vector.push_back (&bobj);
my_vector.push_back (&iobj);
if (my_vector[n].name() == "bool") {
bool* pval = nullptr;
my_vector[n].create((void**) &pval);
*pval = true;
}
The object factory uniformly creates classes that have a uniform interface.
You need to make classes that take a value string and a type string, process them, and behave exactly the same on the outside. Then the fact that data of different types are processed inside them will not matter, and instances of such classes can be collected, for example, in a map with a key-string in which the type will be.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question