我需要创建一个 ID2D1SolidColorBrush
并且我有一个指向 ID2D1Brush
的指针。我使用 ID2D1HwndRenderTarget
创建纯色画笔:
ID2D1Brush* brush = nullptr;
myRenderTarget->CreateSolidColorBrush(color,
reinterpret_cast<ID2D1SolidColorBrush**>(&brush));
上面的代码有效,但我想知道 reinterpret_cast
是否是正确的选择。
最佳答案
你应该使用 QueryInterface
方法来获取任何不同的接口(interface),该接口(interface)不是您获取的接口(interface)的基接口(interface)。另外,我建议使用一些 COM 智能指针,比如 _com_ptr_t
或由 #import
生成的那些或 CComPtr<>
.
更新
好的,我看到了 ID2D1SolidColorBrush
inherits from ID2D1Brush
.在那种情况下,如果我被迫不使用智能指针,我会使用这样的东西:
ID2D1SolidColorBrush * solid_brush = nullptr;
myRenderTarget->CreateSolidColorBrush(color, &solid_brush);
ID2D1Brush * brush = solid_brush;
绝对不要使用reinterpret_cast
.
更新
不要使用 dynamic_cast
转换回派生接口(interface)。使用 QueryInterface
反而。 https://stackoverflow.com/a/1460850/341065稍微解释一下。
关于c++ - 转换正确性 - ID2D1Brush 到 ID2D1SolidColorBrush (DirectX),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27969552/