gpt4 book ai didi

c++ - 初始化基类

转载 作者:行者123 更新时间:2023-11-28 02:19:25 25 4
gpt4 key购买 nike

据我所知,这应该可行,不是吗?

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

但是,模板类构造函数给我一个错误“只有构造函数可以有基/成员初始化列表”。为什么会这样?

最佳答案

我认为您只是在模板结构的定义中有错别字:您编写了 ViewOfFile 而不是 ViewOfFileAs 作为构造函数。

关于c++ - 初始化基类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33059989/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com