gpt4 book ai didi

c++ - 测试 CComPtr 在 COM 中是否为空

转载 作者:太空宇宙 更新时间:2023-11-04 15:16:46 25 4
gpt4 key购买 nike

我对 COM 智能指针类 CComPtr 的使用有疑问。我正在关注 documentation 中的示例并且代码似乎没有在 CComPtr 上调用 CoCreateInstance()(它只是在为其赋值之前对其进行声明)。

所以我写了一个这样的测试程序:

#include "stdafx.h"
#include "atlbase.h"
#include <iostream>

int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<int> myint = nullptr;
if (myint == nullptr) {
std::cout << "yes" << std::endl;
}
return 0;
}

它在 visual-studio 2013 中给出了以下错误:

------ Build started: Project: ConsoleApplication2, Configuration: Debug Win32 ------  ConsoleApplication2.cppc:\program files (x86)\microsoft visual studio 12.0\vc\atlmfc\include\atlcomcli.h(177): error C2227: left of '->Release' must point to class/struct/union/generic type          type is 'int *'          c:\program files (x86)\microsoft visual studio 12.0\vc\atlmfc\include\atlcomcli.h(175) : while compiling class template member function 'ATL::CComPtrBase::~CComPtrBase(void) throw()'          with          [              T=int          ]          c:\users\calvi_000\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp(18) : see reference to function template instantiation 'ATL::CComPtrBase::~CComPtrBase(void) throw()' being compiled          with          [              T=int          ]          c:\program files (x86)\microsoft visual studio 12.0\vc\atlmfc\include\atlcomcli.h(317) : see reference to class template instantiation 'ATL::CComPtrBase' being compiled          with          [              T=int          ]          c:\users\calvi_000\documents\visual studio 2013\projects\consoleapplication2\consoleapplication2\consoleapplication2.cpp(10) : see reference to class template instantiation 'ATL::CComPtr' being compiled========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

为什么将 nullptr 分配给 CComPtr 对象是非法的?有什么方法可以用来检查 CComPtr 是否拥有任何对象?像 if (myint == nullptr) 这样的调用是否足以检查此智能指针是否不拥有任何对象?

最佳答案

Why is it illegal to assign a nullptr to a CComPtr object ?

事实并非如此。正如 Hans 所指出的,CComPtr 只能与 COM 接口(interface)一起使用,int 不是兼容类型。这就是编译器错误告诉您的内容:

error C2227: left of '->Release' must point to class/struct/union/generic type          type is 'int *'

So the problem is not that you are assigning nullptr, but that int does not have a Release() method, which CComPtr requires. An IUnknown (or derived) interface does have a Release() method.

Is there any method that we can use to check whether or not a CComPtr owns any objects ? Will a call like if (myint == nullptr) enough to check if this smart pointer does not own any object ?

If you read the CComPtr documentation more carefully, you would see that CComPtr derives from CComPtrBase, which implements operator!(), operator T*() and operator==() operators (so CComPtr will act like a raw pointer), as well as an IsEqualObject() method:

int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<IUnknown> myUnk = nullptr;
if (!myUnk) { // calls myUnk.operator!() ...
std::cout << "null" << std::endl;
else
std::cout << "not null" << std::endl;
}
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<IUnknown> myUnk = nullptr;
if (myUnk) { // calls myUnk.operator IUnknown*() ...
std::cout << "not null" << std::endl;
else
std::cout << "null" << std::endl;
}
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<IUnknown> myUnk = nullptr;
if (myUnk == nullptr) { // calls myUnk.operator==(IUnknown*) ...
std::cout << "null" << std::endl;
else
std::cout << "not null" << std::endl;
}
return 0;
}

int _tmain(int argc, _TCHAR* argv[])
{
CComPtr<IUnknown> myUnk = nullptr;
if (myUnk.IsEqualObject(nullptr)) {
std::cout << "null" << std::endl;
else
std::cout << "not null" << std::endl;
}
return 0;
}

关于c++ - 测试 CComPtr 在 COM 中是否为空,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30902078/

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