I have a pointer which I wish to point to an LRESULT which I am returning from a function. Currently, to insert the value returned from the function, I am storing it in a temporary variable then making the pointer point to that variable. I want to know if there is a way of doing this without the temporary variable.
Coded example follows:
LRESULT* ret = nullptr;
//---
LRESULT temp = foo();
ret = &temp;
//---
return ret == nullptr ? bar() : *ret;
I have tried using this:
*ret = foo();
But this does not work, as ret is a null pointer and has no assigned memory to put the value in.
I have also tried this:
ret = &(foo());
But this does not work as well.
edit:
for context the full function I am working on is as follows:
LRESULT CALLBACK Window::HandleMsgSetup(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam) noexcept
{
LRESULT* ret = nullptr;
if (msg == WM_NCCREATE)
{
const CREATESTRUCTW* const pCreate = reinterpret_cast<CREATESTRUCTW*>(lParam);
Window* const pWnd = static_cast<Window*>(pCreate->lpCreateParams);
SetWindowLongPtr(hWnd, GWLP_USERDATA, reinterpret_cast<LONG_PTR>(pWnd));
SetWindowLongPtr(hWnd, GWLP_WNDPROC, reinterpret_cast<LONG_PTR>(&Window::HandleMsgThunk));
LRESULT lr = pWnd->HandleMsg(hWnd, msg, wParam, lParam);
ret = &lr;
}
return ret == nullptr ? DefWindowProc(hWnd, msg, wParam, lParam) : *ret;
}
and to once more reiterate, the function works fine in its current state, I merely wish to remove the seemingly unessecery variable lr