從我能收集的信息來看,這應該起作用,不是嗎?初始化基類
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);
}
};
但是,模板類的構造函數給我一個錯誤「只有構造函數可以有基/成員初始化列表」。這是爲什麼發生?
無關,但你爲什麼用'NULL'宏來寫一個整數值? –
您可能想要將模板構造函數從ViewOfFile重命名爲ViewOfFileAs –
@NeilKirk,因爲它應該是0並且像其他宏一樣是紫色的,它的時尚選擇 – user81993