ptr::operator bool

Operator für die Verwendung von com::ptr in einem bedingten Ausdruck.

operator bool();

Rückgabewert

true, wenn das Besitze COM-Objekt gültig ist. Andernfalls false.

Hinweise

Das Besitze COM-Objekt ist gültig, wenn es nicht nullptr ist.

Dieser Operator konvertiert tatsächlich zu _detail_class::_safe_bool, die sicherer als bool ist, da es nicht zu einem ganzzahligen Typ konvertiert werden kann.

Beispiel

In diesem Beispiel implementiert eine CLR-Klasse, die com::ptr verwendet, um den IXMLDOMDocument-Objekt des privaten Members zu umschließen.Die CreateInstance-Memberfunktion verwendet operator bool, nachdem sie das neue Dokumentobjekt erstellt hat, um zu bestimmen, ob sie in die Konsole schreibt und gültig ist, wenn sie sich befindet.

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

Anforderungen

Headerdatei <msclr \ COM \ ptr.h>

Namespace msclr::com

Siehe auch

Referenz

ptr::operator!

Weitere Ressourcen

PTR-Member