Cal*_* Hu 0 c++ com visual-studio-2013
我有一个关于COM智能指针类的用法的问题CComPtr.我下面从一个实例文档,它似乎代码不调用CoCreateInstance()上CComPtr(它只是分配给它的值之前声明它).
所以我写了一个这样的测试程序:
#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;
}
Run Code Online (Sandbox Code Playgroud)
它在visual-studio 2013中出现以下错误:
------ Build started: Project: ConsoleApplication2, Configuration: Debug Win32 ------
ConsoleApplication2.cpp
c:\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)是否足以检查此智能指针是否不拥有任何对象?
为什么将nullptr分配给CComPtr对象是非法的?
它不是.正如Hans所指出的,CComPtr只能与COM接口一起使用,int不是兼容类型.这就是编译器错误告诉你的:
error C2227: left of '->Release' must point to class/struct/union/generic type
type is 'int *'
所以问题不是你要分配nullptr,而是int没有一个Release()方法,这CComPtr需要.一个IUnknown(或衍生的)接口也有一个Release()方法.
有没有什么方法可以用来检查是否
CComPtr拥有任何对象?调用if (myint == nullptr)是否足以检查此智能指针是否不拥有任何对象?
如果你读的CComPtr文档更仔细,你会看到CComPtr派生的CComPtrBase,它实现了operator!(),operator T*()而且operator==()运营商(所以CComPtr会采取行动就像一个原始指针),以及一个IsEqualObject()方法:
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
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;
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2518 次 |
| 最近记录: |