这是我的功能:
void CreateRenderTarget(HWND, ID2D1HwndRenderTarget*);
它是如何工作的:
void D2DRes::CreateRenderTarget(HWND hwnd, ID2D1HwndRenderTarget* pRT)
{
RECT rc;
GetClientRect(hwnd,&rc);
pFactory->CreateHwndRenderTarget(
D2D1::RenderTargetProperties(),
D2D1::HwndRenderTargetProperties(
hwnd,
D2D1::SizeU(
rc.right - rc.left,
rc.bottom - rc.top)),
&pRT);
}
它是这样调用的:
pD2DRes->CreateRenderTarget(map.GetHWnd(),map.GetpRT());
从 CreateRenderTarget() 返回后,map.pRT 为 NULL。怎么来的?
编辑:有我的 GetpRT() 函数:
ID2D1HwndRenderTarget* MapEditor::GetpRT()
{
return pRT;
}
最佳答案
第一个问题:
Once returning from
CreateRenderTarget()
,map.pRT
isNULL
. How comes?
因为您没有通过引用 传递指针。为了通过引用传递它,CreateRenderTarget()
的签名应该是:
void CreateRenderTarget(HWND, ID2D1HwndRenderTarget*&);
// ^
除非您这样做,否则您的 CreateRenderTarget()
将处理作为参数传递的指针的拷贝,并且对其值的更改不会影响原始指针。
第二个问题:
你写:
[...] There is my
GetpRT()
function:ID2D1HwndRenderTarget* MapEditor::GetpRT() { return pRT; }
注意,GetpRT
还应该返回一个 reference 到内部 pRT
指针,否则上述更改不会产生影响(您将通过引用传递原始 pRT
的拷贝。
因此,您应该相应地修改其签名:
ID2D1HwndRenderTarget*& MapEditor::GetpRT()
// ^
{
return pRT;
}
关于c++ - 通过引用传递指针是执行此操作的正确方法吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15742709/