According to Herb Sutter's C++ Coding Standards: 101 Rules, Guidelines, and Best Practices programmer should avoid c-style casting:
C-style casts have different (and often dangerous) semantics depending on context, all disguised behind a single syntax. Replacing C-style casts with C++-style casts helps guard against unexpected errors
I am trying to pass pointer p_ctrl
to WinAPI callback function, for which I want to use DWORD_PTR parameter of callback function (below example is working, but contains C-style casts where commented):
WndCtrls* WndCtrls::Button ( WndCtrls* const p_ctrl, HWND hwnd, RECT const &rc )
{
p_ctrl->ctrl = CreateWindowEx (
0,
L"BUTTON",
p_ctrl->w_classNameButton.c_str (),
WS_VISIBLE | WS_CHILD | BS_OWNERDRAW,
rc.left,
rc.top,
rc.right,
rc.bottom,
hwnd,
0,
(HINSTANCE)GetWindowLongPtr ( hwnd, GWL_HINSTANCE ), // Problematic C-style cast for which I already know workaround
p_ctrl
);
SetWindowSubclass ( p_ctrl->ctrl, WndCtrls::CtrlProc, 0, (DWORD_PTR)p_ctrl ) ) // C-style cast
return p_ctrl;
}
LRESULT CALLBACK WndCtrls::CtrlProc ( HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam, UINT_PTR uIdSubclass, DWORD_PTR dwRefData )
{
WndCtrls* const p_ctrl = (WndCtrls*)dwRefData; // Problematic C-style cast
switch ( message )
{
...
}
return DefSubclassProc ( hwnd, message, wParam, lParam );
}
I already tried dynamic cast, but that gives me errors. reinterpret_cast
should not be used at all (according to Sutter).
Please is there a way to do those casts using c++ provided functions?