确定自己的运算符的 COM 对象是否无效。
bool operator!();
返回值
如果指定的对象无效,则为 true;否则为 false。
备注
如果其不是nullptr,那么所持有的COM 对象是有效的。
示例
本示例实现使用 com::ptr 包装其私有成员 IXMLDOMDocument 对象的 CLR 类。CreateInstance 成员函数使用 operator! 对象确定文档是否已拥有和只创建新的实例,如果对象无效。
// comptr_op_not.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) {
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);
}
}
要求
Header file <msclr\com\ptr.h>
Namespace msclr::com