A wide range of structures is used in Win32 programming. Many times only some of their fields are used and all the other fields are set to zero. For example:
STARTUPINFO startupInfo; // has more than 10 member variables
ZeroMemory( &startupInfo, sizeof( startupInfo ) ); //zero out
startupInfo.cb = sizeof( startupInfo ); //setting size is required according to MSDN
startupInfo.dwFlags = STARTF_FORCEOFFFEEDBACK;
//Now call CreateProcess() passing the startupInfo into it
I want to stop copy-pasting such code and instead use an abstraction that would care about zeroing and setting parameters. Let's presume I only need the struct initialized as in example, and no other tuning is ever needed. Is the following a good solution? What are possible problems?
class CStartupInfo : public STARTUPINFO {
public:
CStartupInfo()
{
ZeroMemory( this, sizeof( STARTUPINFO ) );
cb = sizeof( STARTUPINFO );
dwFlags = STARTF_FORCEOFFFEEDBACK;
}
};
I'm in particular concerned about the ZeroMemory() call - looks like I fully control the code and the class has no vtable and calling ZeroMemory() this way is safe and there's no big difference between the two code snippets except that the latter provides an abstraction. Are there any caveats?