从我能收集到的,这应该有效,不是吗?
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 :(得分:2)
我认为你的模板结构定义中只有一个拼写错误:你写了ViewOfFile
而不是ViewOfFileAs
作为构造函数。
答案 1 :(得分:0)
ViewOfFile(HANDLE hMap, int64_t OffsetB, SIZE_T SizeB, bool WriteAccess)
应该是
ViewOfFileAs(HANDLE hMap, int64_t OffsetB, SIZE_T SizeB, bool WriteAccess)