C++ 类指针字段是否具有变量(某种程度上)类型?

C++ 类指针字段是否具有变量(某种程度上)类型?,c++,C++,我有一个名为“位图”的模板结构,如下所示: enum PixelOption { I8, I16, I32, I64, F32, F64 }; template <PixelOption T> struct PixelOptionType; template<> struct PixelOptionType < I8 > { using type = uint8_t; }; template<> struct PixelOptionType &l

我有一个名为“位图”的模板结构,如下所示:

enum PixelOption { I8, I16, I32, I64, F32, F64 };

template <PixelOption T> struct PixelOptionType;
template<> struct PixelOptionType < I8 > { using type = uint8_t; };
template<> struct PixelOptionType < I16 > { using type = uint16_t; };
template<> struct PixelOptionType < I32 > { using type = uint32_t; };
template<> struct PixelOptionType < I64 > { using type = uint64_t; };
template<> struct PixelOptionType < F32 > { using type = float; };
template<> struct PixelOptionType < F64 > { using type = double; };

template <PixelOption T>
struct Bitmap {
    using type = typename PixelOptionType<T>::type;

    uint32_t Width, Height;
    type* pData;

    Bitmap(uint32_t Width, uint32_t Height, uint32_t X, uint32_t Y, uint32_t SourceWidth, void* pData) {
        this->Width = Width; this->Height = Height; 
        this->pData = &reinterpret_cast<type*>(pData)[SourceWidth * Y + X];
    }

    type* Pixel(const uint32_t &X, const uint32_t &Y) {
        return &pData[Width * Y + X];
    }
};
枚举像素选项{I8、I16、I32、I64、F32、F64};
模板结构PixelOptionType;
模板结构PixelOptionType{using type=uint8\u t;};
模板结构PixelOptionType{using type=uint16_;};
模板结构PixelOptionType{using type=uint32_;};
模板结构PixelOptionType{using type=uint64_;};
模板结构PixelOptionType{using type=float;};
模板结构PixelOptionType{using type=double;};
模板
结构位图{
使用type=typename PixelOptionType::type;
uint32_t宽度、高度;
类型*pData;
位图(uint32_t宽度、uint32_t高度、uint32_t X、uint32_t Y、uint32_t源宽度、void*pData){
此->宽度=宽度;此->高度=高度;
这->pData=&重新解释(pData)[SourceWidth*Y+X];
}
类型*像素(常数uint32\U t&X、常数uint32\U t&Y){
返回和pData[宽度*Y+X];
}
};
现在我想在名为“通道”的结构中包含这些位图指针的向量,类似于

struct Channel {
    std::vector<Bitmap*> Fragments;
}
结构通道{ std::载体片段; } 但是编译器希望我为指针声明模板参数。一个通道中的所有位图无论如何都是相同的类型(因此是通道),但向通道结构添加一个模板参数实际上只会解决问题,因为我计划在即将到来的“层”结构中包含一个通道向量,并且将面临相同的问题

我想在通道结构的构造函数参数中包含像素选项,但在没有运行时强制转换(我希望避免)的情况下,似乎无法找到通过向量声明的方法

我尝试用伪函数创建一个结构“BitmapBase”,并在位图中继承它,但通过创建BitmapBase向量,在其中存储位图对象并调用pixel,我只得到了伪函数结果,而不是(如我所希望的)替换实函数结果


有人知道如何处理这个问题吗?

如果您选择
位图库
路线,您需要使
位图库::Pixel
虚拟
。另外,如果问题只是关于
std::vector
的不便,您可以使用。例如:

using <template T> BitmapPtrVec = std::vector<Bitmap<T>*>
使用BitmapPtrVec=std::vector

将像素虚拟化成功!谢谢。注意:由于位图有不同的返回类型,使用bitmapbase它将返回虚拟虚拟函数类型,但所有通道返回类型都是相同的,因此我可以在没有问题的情况下使用它,所以我最终还是将问题向前推。