我對使用COM智能指針類CComPtr
有疑問。我正在關注documentation的一個示例,看起來代碼不會在CComPtr
上調用CoCreateInstance()
(它只是在分配值之前聲明它)。測試COM中的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;
}
而且它提供了以下錯誤在視覺工作室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)
這樣的調用是否足以檢查這個智能指針是否不擁有任何對象?
但是CComPtr爲COM接口指針智能指針包裝。從IUnknown派生的那種。使用* int *沒有任何意義。你會得到編譯錯誤,因爲* int *沒有Release()方法。 CComPtr在其析構函數中自動調用智能部分。 –
但我如何檢查CComPtr是否擁有任何對象?只是一個「if(ccomPtr)」會做的伎倆? –
你已經知道如何做到這一點,你沒有錯的一件事。 –