代码之家  ›  专栏  ›  技术社区  ›  Megidd

对结构的成员使用泛型类型

  •  0
  • Megidd  · 技术社区  · 7 年前

    struct :

    struct MyStruct {
    
        const unsigned short *ptr = nullptr;
    
        /* above pointer might also be:
        const unsigned int   *ptr = nullptr; 
        */
    
        // ...
    };
    

    我用的是 结构 作为一个班级的成员:

    class MyClass
    {
        // ...
    
    private:
        MyStruct m_struct;
    }
    

    MyClass ,我希望能够有这样的开关:

    void MyClass::myFunc()
    {
        // ...
    
        switch(myDataType) {
    
        case USHORT:
            m_struct.ptr = reinterpret_cast<const unsigned short *>(myDataPtr);
            break;
    
        case UINT:
            m_struct.ptr = reinterpret_cast<const unsigned int   *>(myDataPtr);
            break;
    
        default:
            break;
    
        }
    }
    

    目前,上述 switch 由于类型不兼容,因此不可能。

    结构 template ,以便我能够执行上述操作 转换 关于类成员函数。

    1 回复  |  直到 7 年前
        1
  •  4
  •   songyuanyao    7 年前

    你可以把它们都做成类模板。那就不用用了 switch . 例如

    template <typename T>
    struct MyStruct {
    
        const T *ptr = nullptr;
    
        // ...
    };
    
    template <typename T>
    class MyClass
    {
        // ...
    
    private:
        MyStruct<T> m_struct;
    };
    
    template <typename T>
    void MyClass<T>::myFunc()
    {
        // ...
    
        m_struct.ptr = reinterpret_cast<const T *>(myData);
    }
    
        2
  •  1
  •   Jarod42    7 年前

    std::variant

    struct MyStruct {
    
        std::variant<const unsigned short*, const unsigned int*> ptr{std::in_place_index_t<0>, nullptr}; 
    
        // ...
    };
    

    然后

    void MyClass::myFunc()
    {
        // ...
    
        switch(myDataType) {
        case USHORT:
            m_struct.ptr = reinterpret_cast<const unsigned short *>(myDataPtr);
            break;
    
        case UINT:
            m_struct.ptr = reinterpret_cast<const unsigned int *>(myDataPtr);
            break;
    
        default:
            break;
        }
    
        // ...
    }
    

    m_struct.ptr = myDataPtr; 如果 myDataPtr 是一个 标准::变量

    std::visit 可能也有帮助而不是 switch .

        3
  •  0
  •   Megidd    7 年前

    T 在编译时。因此,我目前正在使用以下解决方法。我不确定这是否是最好的:

    struct MyStruct {
    
        const unsigned short *ptr_ushort = nullptr;
    
        // consider the other type:
        const unsigned int   *ptr_uint   = nullptr; 
    
        // To know what type we have:
        TypeEnum ptr_type;
    
        // ...
    };
    

    然后,我像这样实现了我的切换:

    void MyClass::myFunc()
    {
        // ...
    
        switch(myDataType) {
    
        case USHORT:
            m_struct.ptr_ushort = reinterpret_cast<const unsigned short *>(myDataPtr);
            m_struct.ptr_type   = TypeEnum::USHORT;
            break;
    
        case UINT:
            m_struct.ptr_uint   = reinterpret_cast<const unsigned int   *>(myDataPtr);
            m_struct.ptr_type   = TypeEnum::UINT;
            break;
    
        default:
            break;
    
        }
    
        // ...
    }