user81993
user81993

Reputation: 6613

Initializing a base class

From what I could gather, this should work, no?

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

however, the template class constructor gives me an error "Only a constructor can have base/member initializer list". Why is this happening?

Upvotes: 0

Views: 45

Answers (2)

NathanOliver
NathanOliver

Reputation: 180500

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

Should be

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

Upvotes: 0

Unda
Unda

Reputation: 1899

I think you just have a typo in the definition of your template struct: you wrote ViewOfFile instead of ViewOfFileAs as a constructor.

Upvotes: 2

Related Questions