Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ ComPtr.As()做什么?_C++_Com - Fatal编程技术网

C++ ComPtr.As()做什么?

C++ ComPtr.As()做什么?,c++,com,C++,Com,我正在通过一些示例学习DirectX 12,但是我很难理解ComPtr.As()方法的作用 ComPtr<ID3D12Device> device; ComPtr<ID3D12Device> device2; // Do Stuff with Device device.As(&device2); // What does this do? ComPtr装置; 计算机设备2; //用这个设备做事 设备组件(和设备2);//这有什么用? 您在哪里找到此示例代码

我正在通过一些示例学习DirectX 12,但是我很难理解ComPtr.As()方法的作用

ComPtr<ID3D12Device> device;
ComPtr<ID3D12Device> device2; 

// Do Stuff with Device
device.As(&device2); // What does this do?
ComPtr装置;
计算机设备2;
//用这个设备做事
设备组件(和设备2);//这有什么用?

您在哪里找到此示例代码?看起来很可疑。这是对
As
的一种无意义的使用。如果将其扩展到底层的
QueryInterface
,也同样愚蠢:

hr = device->QueryInterface( IID_PPV_ARGS(device2) );
事实上,您的代码最好编写为:

device2 = device;
通常使用
As
从现有接口获取新接口。例如,使用Direct3D 11,您将设备创建为Direct3D 11.0接口,然后必须
QueryInterface
11.1、11.2、11.3和/或11.4版本才能使用它们:

ComPtr<ID3D11Device> device;
DX::ThrowIfFailed(D3D11CreateDevice(..., device.ReleaseAndGetAddressOf());

ComPtr<ID3D11Device2> device2;
hr = device.As(&device2);
if (FAILED(hr))
    // Do whatever handling you do if the system doesn't support 11.2
ComPtr装置;
DX::ThrowIfFailed(D3D11CreateDevice(…,device.ReleaseAndGetAddressOf());
计算机设备2;
hr=设备组件(和设备2);
如果(失败(小时))
//如果系统不支持11.2,请执行任何处理
Direct3D 11的另一个常见用途是处理可能不存在的调试接口:

#ifndef NDEBUG
    ComPtr<ID3D11Debug> d3dDebug;
    if (SUCCEEDED(device.As(&d3dDebug)))
    {
        ComPtr<ID3D11InfoQueue> d3dInfoQueue;
        if (SUCCEEDED(d3dDebug.As(&d3dInfoQueue)))
        {
#ifdef _DEBUG
            d3dInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_CORRUPTION, true);
            d3dInfoQueue->SetBreakOnSeverity(D3D11_MESSAGE_SEVERITY_ERROR, true);
#endif
            D3D11_MESSAGE_ID hide [] =
            {
                D3D11_MESSAGE_ID_SETPRIVATEDATA_CHANGINGPARAMS,
                // TODO: Add more message IDs here as needed.
            };
            D3D11_INFO_QUEUE_FILTER filter = {};
            filter.DenyList.NumIDs = _countof(hide);
            filter.DenyList.pIDList = hide;
            d3dInfoQueue->AddStorageFilterEntries(&filter);
        }
    }
#endif
\ifndef NDEBUG
ComPtr-d3dDebug;
if(成功(device.As(&d3dDebug)))
{
ComPtr-d3dInfoQueue;
if(成功(d3dDebug.As(&d3dInfoQueue)))
{
#ifdef_调试
d3dInfoQueue->SetBreakOnSeverity(D3D11_消息_严重性_损坏,true);
d3dInfoQueue->SetBreakOnSeverity(D3D11_消息_严重性_错误,true);
#恩迪夫
D3D11_消息_ID隐藏[]=
{
D3D11_消息_ID_设置私有数据_更改参数,
//TODO:根据需要在此处添加更多消息ID。
};
D3D11_INFO_QUEUE_FILTER={};
filter.DenyList.NumIDs=\u countof(隐藏);
filter.DenyList.pIDList=hide;
d3dInfoQueue->AddStorageFilterEntries(&filter);
}
}
#恩迪夫
同样重要的是,不要忽略从
As
返回的HRESULT返回值。应使用
successed
FAILED
宏或类似fast fail的宏


请参阅。

涵盖了一些要点,并表明它可以帮助您避免进行
查询接口
、丑陋的强制转换和引用计数。为什么需要进行
查询接口
是一个单独的问题……这意味着您需要找到更好的示例。