gpt4 book ai didi

c++ - 使用 QueryInterface 方法的目的是什么? (Direct3D)

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:35:40 48 4
gpt4 key购买 nike

我明白 QueryInterface 是什么方法实际上是这样做的——它只返回一个指向特定接口(interface)的指针。但我的问题是,我为什么要使用这种方法?

我的意思是,有什么区别

QueryInterface(__uuidof(IDXGIResource), (void**)&Resource)

IDXGIResource * 资源

这些不是很相似吗?如果是这样,为什么我什至需要使用该方法?我应该出于什么原因使用它?

最佳答案

COM 假定单个对象将提供多个接口(interface),即接口(interface)将是细粒度的,您将希望一次使用多个接口(interface)。 QueryInterface 是您获取指向对象上其他接口(interface)的指针的方式。

您的示例不完整。第一个没有显示正在从现有接口(interface)调用 QueryInterface,第二个没有为指针分配任何值(它是一个未初始化的指针)。一个实际的例子将两者结合起来:

IDXGIResource * Resource = NULL;
pInterface->QueryInterface(__uuidof(IDXGIResource), (void **)&Resource);

为了稳健,您应该确保 QueryInterface 调用 succeeded在尝试使用指针之前。

if (SUCCEEDED(pInterface->QueryInterface(__uuidof(IDXGIResource), (void **)&Resource))
Resource->DoSomething();

关于c++ - 使用 QueryInterface 方法的目的是什么? (Direct3D),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31821754/

48 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com