Declare variables at top of function or in separate scopes?

后端 未结 9 1131
温柔的废话
温柔的废话 2020-12-01 01:47

Which is preferred, method 1 or method 2?

Method 1:

LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
    switch (         


        
9条回答
  •  Happy的楠姐
    2020-12-01 02:03

    I like Method 3:

    LRESULT wpMainWindowPaint(HWND hwnd)
    {
        HDC hdc;
        PAINTSTRUCT ps;
    
        RECT rc;
        GetClientRect(hwnd, &rc);           
    
        hdc = BeginPaint(hwnd, &ps);
        // drawing here
        EndPaint(hwnd, &ps);
        return 0;
    }
    
    LRESULT CALLBACK wpMainWindow(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
    {
        switch (msg)
        {
            case WM_PAINT:      return wpMainWindowPaint(hwnd);
            default:            return DefWindowProc(hwnd, msg, wparam, lparam);
        }
    }
    

    If it deserves its own scope for organization purposes, it deserves its own function. If you're worried about function call overhead, make it inline.

提交回复
热议问题