2015-02-10 62 views
0

我有一個名爲「位圖」模板結構,看起來像這樣:具有變量(有點)類型的類指針字段?

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]; 
    } 
}; 

現在我想包括在結構中的位圖指針被稱爲「通道」的載體,八九不離十像

struct Channel { 
    std::vector<Bitmap*> Fragments; 
} 

但編譯器希望我爲指針聲明模板參數。通道中的所有位圖都是相同的類型(因此通道),但將模板參數添加到通道結構中除了將問題向前推,因爲我打算在即將到來的通道中包含通道向量「層」結構,並將面臨同樣的問題。

我想包括通道結構的構造參數中的像素選項,但似乎找不到我的方式通過該向量聲明沒有運行時投射(我希望避免)。

我試圖用虛函數創建一個struct「BitmapBase」,並在位圖中繼承它,但是通過創建一個位圖基向量,在其中存儲位圖對象並調用像素,我只獲得了虛函數結果,而不是(如我所希望的)替代真實的功能結果。

有沒有人有一個想法如何處理這個?

+0

使像素虛擬做到了訣竅!謝謝。 – user81993 2015-02-10 16:46:10

回答

0

如果你去BitmapBase路線,你需要使BitmapBase::Pixelvirtual。另外,如果問題只是std::vector<Bitmap<T>*>的不便之處,您可以使用template aliases。例如:

using <template T> BitmapPtrVec = std::vector<Bitmap<T>*> 
+0

注意:由於位圖有不同的返回類型,並且使用位圖庫,它會返回虛擬虛擬函數類型,但所有通道返回類型都是相同的,所以我可以在那裏使用它沒有任何問題,所以我最終推動了問題。 – user81993 2015-02-10 17:03:15

相關問題