2015-10-10 167 views
0

從我能收集的信息來看,這應該起作用,不是嗎?初始化基類

struct ViewOfFile { 
    void* p = nullptr; 

    ViewOfFile(HANDLE hMap, int64_t OffsetB, SIZE_T SizeB, bool WriteAccess) { 
     DWORD ViewAccessMode = FILE_MAP_READ | (WriteAccess ? FILE_MAP_WRITE : NULL); 
     LARGE_INTEGER LI = { OffsetB }; 
     p = MapViewOfFile(hMap, ViewAccessMode, LI.HighPart, LI.LowPart, SizeB); 
     if (p == nullptr) throw Exception("ViewOfFile: Failed to create view."); 
    } 
    ~ViewOfFile() { 
     if (p) UnmapViewOfFile(p); 
    } 
    operator void*() const { return p; } 
    operator char*() const { return reinterpret_cast<char*>(p); } 
}; 

template <typename T> 
struct ViewOfFileAs : ViewOfFile { 
    T* as; 
    ViewOfFile(HANDLE hMap, int64_t OffsetB, SIZE_T SizeB, bool WriteAccess) : ViewOfFile(hMap, OffsetB, SizeB, WriteAccess) { 
     as = reinterpret_cast<T*>(p); 
    } 
}; 

但是,模板類的構造函數給我一個錯誤「只有構造函數可以有基/成員初始化列表」。這是爲什麼發生?

+0

無關,但你爲什麼用'NULL'宏來寫一個整數值? –

+0

您可能想要將模板構造函數從ViewOfFile重命名爲ViewOfFileAs –

+0

@NeilKirk,因爲它應該是0並且像其他宏一樣是紫色的,它的時尚選擇 – user81993

回答

2

我想你只是在模板結構的定義中有一個錯字:你寫了ViewOfFile而不是ViewOfFileAs作爲構造函數。

+0

doh! :捂臉。 – user81993

0
ViewOfFile(HANDLE hMap, int64_t OffsetB, SIZE_T SizeB, bool WriteAccess) 

應該

ViewOfFileAs(HANDLE hMap, int64_t OffsetB, SIZE_T SizeB, bool WriteAccess)