I have an OVERLAPPED
I am trying to put in a known good state. OVERLAPPED
is a Windows typedef'd structure provided by the Win32 API. I can't change it.
#pragma push_macro ("WIN32_LEAN_AND MEAN")
#pragma push_macro ("NOMINMAX")
#define WIN32_LEAN_AND MEAN
#define NOMINMAX
#include <windows.h>
struct OverlappedIo : public OVERLAPPED
{
explicit OverlappedIo()
: Internal(0), InternalHigh(0), Offset(0), OffsetHigh(0)
, Pointer(NULL), hEvent(hEvent) { }
...
};
#pragma pop_macro ("NOMINMAX")
#pragma pop_macro ("WIN32_LEAN_AND MEAN")
Under MinGW, it results in (it will probably have issues under native Win32; I have not started testing the platform):
g++ -DNDEBUG -g -O2 -march=native -pipe -c network.cpp
In file included from network.cpp:4:0:
network.h: In constructor 'OverlappedIo::OverlappedIo()':
network.h:244:5: error: class 'OverlappedIo' does not have any field n
amed 'Internal'
: Internal(0), InternalHigh(0), Offset(0), OffsetHigh(0)
^
...
I also tried with _OVERLAPPED
with the same result. And when I try to provide the missing constructor to perform the C++ initialization (I did not expect this to work, but it was worth a try):
//! OVERLAPPED I/O
OVERLAPPED::OVERLAPPED()
: Internal(0), InternalHigh(0), Offset(0), OffsetHigh(0), hEvent(NULL) { }
It results in:
In file included from network.cpp:4:0:
network.h:24:24: error: ISO C++ forbids declaration of 'OVERLAPPED' with no type
[-fpermissive]
OVERLAPPED::OVERLAPPED()
^
In C++11, I believe I can initialize with curly braces. But its not clear to me how to do it in C++03. Also note this is a simplified example, and the real class is more complex and its trying to provide stronger exception safety. Because of the stronger exception safety, I want to initialize the field, and not memset
them or assign them. (Sorry about the word-smithing for "initialize" and "assign").
Is it possible to initialize a typedef'd structure from C?
If so, how do I initialize the fields of the structure in C++03?
Related, here's the struct from <windows.h>
:
typedef struct _OVERLAPPED {
ULONG_PTR Internal;
ULONG_PTR InternalHigh;
union {
struct {
DWORD Offset;
DWORD OffsetHigh;
} DUMMYSTRUCTNAME;
PVOID Pointer;
} DUMMYUNIONNAME;
HANDLE hEvent;
} OVERLAPPED, *LPOVERLAPPED;
This is related to Are members of a C++ struct initialized to 0 by default? and Correct way of initializing a struct in a class constructor. Its also somewhat related to Do the parentheses after the type name make a difference with new? (I'm trying to avoid the silly rules and simply put an object in a known good state).