条件式で com::ptr を使用する演算子。
operator bool();
戻り値
所有された COM オブジェクトが有効な場合true ; 別の方法で false 。
解説
所有された COM オブジェクトは、 nullptrでない場合は有効です。
整数型に変換できないため bool より安全であるこの演算子は _detail_class::_safe_bool 実際に変換します。
使用例
この例では、プライベート メンバー IXMLDOMDocument オブジェクトをラップするために com::ptr を使用する CLR クラスを実装します。新しいドキュメントのオブジェクトの場合有効であると書き込みを作成した後 CreateInstance のメンバー関数の使用 operator bool コンソールにするかどうかを判断します。
// comptr_op_bool.cpp
// compile with: /clr /link msxml2.lib
#include <msxml2.h>
#include <msclr\com\ptr.h>
#import <msxml3.dll> raw_interfaces_only
using namespace System;
using namespace System::Runtime::InteropServices;
using namespace msclr;
// a ref class that uses a com::ptr to contain an
// IXMLDOMDocument object
ref class XmlDocument {
public:
void CreateInstance(String^ progid) {
if (!m_ptrDoc) {
m_ptrDoc.CreateInstance(progid);
if (m_ptrDoc) { // uses operator bool
Console::WriteLine("DOM Document created.");
}
}
}
// note that the destructor will call the com::ptr destructor
// and automatically release the reference to the COM object
private:
com::ptr<IXMLDOMDocument> m_ptrDoc;
};
// use the ref class to handle an XML DOM Document object
int main() {
try {
XmlDocument doc;
// create the instance from a progid string
doc.CreateInstance("Msxml2.DOMDocument.3.0");
}
catch (Exception^ e) {
Console::WriteLine(e);
}
}
必要条件
ヘッダー ファイル <msclr\com\ptr.h>
名前空間 msclr::com