Hello. There was a need to implement a dynamic structure (linear) filled with class template objects, but since the type parameters are different, I created a dynamic structure of pointers to void to fill in a dynamic structure using the function.

But now the question arose: is there a way to get access to the desired object? Or is it possible to implement this task more easily?

Code:

struct InfoForUI { std::string name; void* VectorClass; }; struct ListForClass { InfoForUI info; ListForClass* next; }; class UIforClass { public: ListForClass *head; UIforClass() { head = NULL; } ~UIforClass(); void Add() { //тут интерфейсная часть Vector <char> *obj = new Vector <char>; std::cin >>*obj; temp->info.VectorClass = obj; } }; 

Forgive help advice.

  • one
    Unclear. Do you want to create a unidirectional list that will hold a pointer to an arbitrary type and some name? If yes, then to search for the item you need to go through the list sequentially. You can enter a hash table and speed up this search. You can store elements in an array, as in std::vector . And what STL does not like? - SuperDimon007
  • SuperDimin007, you can take any dynamic structure. The main question I had during the extraction of the object itself. How can I refer to the methods of this class? And is a simpler implementation possible? - rankery
  • I, like, listed the main methods of extraction. You can still use trees. - SuperDimon007
  • Most likely, your problem can be solved differently. But not this one, but the original one. Therefore, if not a secret - why do you need such a way of storage? What do you want to do with these objects? Why should they all be stored in one collection? In fact, your task is approximately the same - there is an array of pointers to int , double and others, and then you need, for example, for the k -th element to determine the type, and not only to determine, dynamically at runtime ... I think it is necessary change the approach itself. Is it possible, for example, to reduce the original task to the storage of class hierarchy objects with virtual methods? - Harry
  • @Harry, I create an interface for demonstrating a class template with the creation of its objects, as well as changing them using the methods of the class itself. Actually, I decided to select a structure for this, but since I doubtfully saw the definition of a type parameter in this structure, I decided to use a null pointer. - rankery

1 answer 1

Frankly speaking, nothing better was invented than to make a base abstract class, in which you can add virtual functions that you want to work with, and keep addresses in the list for the base class. Something like

 class Base { public: Base() {} virtual ~Base() {} virtual void out() const = 0; }; template<typename T> class Test :public Base { public: Test(T t):t(t){ cout << __func__ << "@" << t << endl; } ~Test(){ cout << __func__ << "@" << t << endl; } void out() const { cout << __func__ << "@" << t << endl; } private: T t; }; int main(int argc, const char * argv[]) { Test<int> t(5); Test<double> v(6.6); vector<Base*> b {&t, &v}; for(auto x: b) x->out(); } 

If you wish, you can also drag a dynamic_cast , if you really want to send parameters of the corresponding type ...