Storing a type in C++

后端 未结 8 1442
春和景丽
春和景丽 2020-12-05 17:41

Is it possible to store a type name as a C++ variable? For example, like this:

type my_type = int; // or string, or Foo, or any other type
void* data = ...;
         


        
8条回答
  •  我在风中等你
    2020-12-05 17:56

    A better process is to have a common base class containing a load method, and an interface for loaders. This would allow other parts of the program to load data generically without knowledge of the descendant class:

    struct Load_Interface;
    
    struct Loader
    {
      virtual void visit(Load_Interface&) = 0;
    }
    
    struct Load_Interface
    {
      virtual void accept_loader(Loader& l)
        {
            l.visit(*this);
        }
    };
    

    This design avoids the need to know the types of objects.

提交回复
热议问题