2015-06-17 25 views
-1

我對使用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 ========== 

爲什麼非法分配nullptrCComPtr對象?有什麼方法可以用來檢查CComPtr是否擁有任何物品?像if (myint == nullptr)這樣的調用是否足以檢查這個智能指針是否不擁有任何對象?

+5

但是CComPtr爲COM接口指針智能指針包裝。從IUnknown派生的那種。使用* int *沒有任何意義。你會得到編譯錯誤,因爲* int *沒有Release()方法。 CComPtr在其析構函數中自動調用智能部分。 –

+0

但我如何檢查CComPtr是否擁有任何對象?只是一個「if(ccomPtr)」會做的伎倆? –

+0

你已經知道如何做到這一點,你沒有錯的一件事。 –

回答

6

爲什麼將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 documentation,你會看到CComPtrCComPtrBase,它實現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; 
} 

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; 
}