Steve A
Steve A

Reputation: 2013

Visual Studio C26462 Code Analysis Warning with HWND

In a Visual Studio MFC app, I have the following function:

BOOL CSomeDialog::OnToolTipNotify(UINT id, NMHDR* pNMHDR, LRESULT* pResult)
{
    ...

    // Get the window handle ...
    const HWND hControl = (HWND)pNMHDR->idFrom;

    ...
}

Visual Studio's code analysis reports:

Warning C26462  The value pointed to by 'hControl' is assigned only once, mark it as a pointer to const (con.4).

I do like code analysis to report on variables that can be consts (I think it clarifies the code's intent), however I don't understand why the above code generates this message. I suspect I am declaring a const pointer rather than a pointer to a const, but I'm unsure of the fix (I've tried "HWND const hControl").

Thanks.

Upvotes: 1

Views: 531

Answers (1)

Bix
Bix

Reputation: 21

This is what I would do:

#define CHWND const HWND__ * const
CHWND hControl = (CHWND)pNMHDR->idFrom;

Alternatively you could this:

typedef const HWND__* const CHWND;
CHWND hControl = (CHWND)pNMHDR->idFrom;

Or this:

using CHWND = const HWND__* const;
CHWND hControl = (CHWND)pNMHDR->idFrom;

Upvotes: 2

Related Questions