c++ - 转换正确性 - ID2D1Brush 到 ID2D1SolidColorBrush (DirectX)

标签 c++ c++11 casting directx direct2d

我需要创建一个 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/

相关文章:

c++ - 在容器中存储模板化派生类

c++ - 如何在 Eclipse CDT 中启用 C++11/C++0x 支持?

c++ - 如何在迭代时从 map 中删除?

c - 当 double 转换为 int 时,最后一位数字丢失

arrays - 转换 unnest() 的结果时 NULL 元素丢失

c++ - 有没有办法创建一个公共(public)输出流对象以在控制台上打印并打印到 C++ 中的文件?

c++ - 是否有交换 C++ vector 中的两个段的函数?

c++ - 有什么办法可以在 utf-8 和平台无关的纯字符串之间进行转换吗?

c++ - 重新解释与 unsigned char* 和 char* 之间的转换

c++ - 在 Visual Studio 中构建解决方案时出现 "Unable to create directory"错误 "Access to path is denied."