Sdílet prostřednictvím


ptr::operator=

Připojí k objektu COM com::ptr.

ptr<_interface_type> % operator=(
   _interface_type * _right
);

Parametry

  • _right
    Ukazatel rozhraní modelu COM pro připojení.

Vrácená hodnota

Sledovací odkaz na com::ptr.

Výjimky

Pokud com::ptr odkaz na objekt COM již vlastní operator= vyvolá InvalidOperationException.

Poznámky

Přiřazení k objektu COM com::ptr odkazuje na objekt modelu COM, ale neuvolní volajícího na něj odkaz.

Tento operátor má stejný účinek jako Attach.

Příklad

Tento příklad implementuje třídu CLR, která používá com::ptr jeho soukromý člen zalomení IXMLDOMDocument objektu. ReplaceDocument První volání členské funkce Release v jakékoli dříve vlastní objekt a poté použije operator= připojit nový objekt dokumentu.

// comptr_op_assign.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:
   // construct the internal com::ptr with a null interface
   // and use CreateInstance to fill it
   XmlDocument(String^ progid) {
      m_ptrDoc.CreateInstance(progid);   
   }

   // replace currently held COM object with another one
   void ReplaceDocument(IXMLDOMDocument* pDoc) {
      // release current document object
      m_ptrDoc.Release();
      // attach the new document object
      m_ptrDoc = pDoc;
   }

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

// unmanaged function that creates a raw XML DOM Document object
IXMLDOMDocument* CreateDocument() {
   IXMLDOMDocument* pDoc = NULL;
   Marshal::ThrowExceptionForHR(CoCreateInstance(CLSID_DOMDocument30, NULL,
      CLSCTX_INPROC_SERVER, IID_IXMLDOMDocument, (void**)&pDoc));
   return pDoc;
}


// use the ref class to handle an XML DOM Document object
int main() {
   IXMLDOMDocument* pDoc = NULL;

   try {
      // create the class from a progid string
      XmlDocument doc("Msxml2.DOMDocument.3.0");

      // get another document object from unmanaged function and
      // store it in place of the one held by the ref class
      pDoc = CreateDocument();
      doc.ReplaceDocument(pDoc);
      // no further need for raw object reference
      pDoc->Release();
      pDoc = NULL;
   }
   catch (Exception^ e) {
      Console::WriteLine(e);   
   }
   finally {
      if (NULL != pDoc) {
         pDoc->Release();
      }
   }
}

Požadavky

Soubor hlaviček<msclr\com\ptr.h>

Obor názvů msclr::com

Viz také

Referenční dokumentace

ptr::Attach

ptr::Detach

ptr::Release

Další zdroje

ptr – členové