This class calls CreateFile, CreateFileMapping, and MapViewOfFile for you. It’s an RAII class that cleans up automatically, thus avoiding leaks even though there’s no garbage collection or smart pointer stuff in these old win32 APIs.
RAII classes were one of the first features that Stroustrup built into C++ when he started to design it in the 1980s. I was a C programmer then and I remember vividly how powerful and advantageous RAII was compared to C. Nowadays most programmers have never had to use plain C and they may tend to take the power of RAII (and other functionally equivalent mechanisms) for granted. But when we program against win32 APIs, we’re back in the world of plain C, and we can notice that RAII classes are easy, simple, and incredibly valuable as thin wrappers around win32 APIs as shown in this example.
This page was first published on June 1, 2021 and last revised on June 1, 2021.