Storing a type in C++

后端 未结 8 1390
春和景丽
春和景丽 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:45

    You can't do that in C++, but you can use the boost any library then test for the type it holds. Example:

    bool is_int(const boost::any & operand)
    {
      return operand.type() == typeid(int);
    }
    

    http://www.boost.org/doc/libs/1_42_0/doc/html/any/s02.html

    0 讨论(0)
  • 2020-12-05 17:49

    Not as written, but you could do something similar...

    class Type
    {
        public:
            virtual ~Type(){}
            virtual void* allocate()const=0;
            virtual void* cast(void* obj)const=0;
    };
    
    template<typename T> class TypeImpl : public Type
    {
          public:
             virtual void* allocate()const{ return new T; }
             virtual void* cast(void* obj)const{ return static_cast<T*>(obj); }
    };
    
    // ...
    Type* type = new TypeImpl<int>;
    void* myint = type->allocate();
    // ...
    

    This kind of thing can be extended depending on what features you need.

    0 讨论(0)
  • 2020-12-05 17:54

    Yes, if you code it yourself.

    enum Foo_Type{
        AFOO,
        B_AFOO,
        C_AFOO,
        RUN
    };
    
    struct MyFoo{
        Foo_Type m_type;
        Boost::shared_ptr<Foo> m_foo;
    }
    

    as commented below, what I left out was that all these "foo" types would have to be related to Foo. Foo would, in essence, be your interface.

    0 讨论(0)
  • 2020-12-05 17:55

    Today I had a similar problem while coding:
    I had the need to store a polymoriphic data type (here named refobj) over wich call functions of the concrete classes implementing it. I need a solution that doesn't cast the variable explicitly because I need to reduce the amount of code.

    My solution (but I haven't tested it yet) looks similar to a previous answer. Actually is quite an experimental solution. It look like this...

    // interface to use in the function
    
    class Type   
    {
    public:
        virtual void* getObj()const=0;
    };
    
    // here the static_cast with the "stored" type
    
    template<typename T> class TypeImpl : public Type
    {
    public:
        TypeImpl(T *obj) {myobj=obj;}
        virtual void* getObj()const{ return static_cast<T*>(myobj); }
    
    private: 
        T* myobj;
    };
    
    // here the type that will contain the polimorific type
    // that I don't want to cast explicitly in my code
    Type *refobj;
    
    // here the "user code "
    void userofTypes()
    {
        ( refobj->getObj() ).c_str(); 
        // getObj() should return a string type over which
        // calling string concrete functions ...let's try!
    }
    
    void main()
    {
        refobj=new TypeImpl < string > ( new string("hello") );
        userofTypes();
    }
    // it might seem absurd don't cast refobj explicitly, but of
    // course there are situation in which it can be useful!
    
    0 讨论(0)
  • 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.

    0 讨论(0)
  • 2020-12-05 18:06

    No you can't store the type directly as you want, but you can instead store the name of the type.

    const char* str = typeid(int).name();
    

    I guess whenever you planned to use that variable for comparison, you could instead at that time compare the str variable against the name() of the types.

    const char* myType = typeid(int).name();
    
    //....
    
    //Some time later:
    if(!strcmp(myType, typeid(int).name()))
    {
      //Do something
    }
    

    More info available here

    0 讨论(0)
提交回复
热议问题