Partilhar via


auto_gcroot::Attach

Anexar auto_gcroot a um objeto.

auto_gcroot<_element_type> & attach(
   _element_type _right
);
auto_gcroot<_element_type> & attach(
   auto_gcroot<_element_type> & _right
);
template<typename _other_type>
auto_gcroot<_element_type> & attach(
   auto_gcroot<_other_type> & _right
);

Parâmetros

  • _right
    O objeto para anexar, ou um auto_gcroot que contém o objeto para anexar.

Valor de retorno

O atual auto_gcroot.

Comentários

Se _right é um auto_gcroot, ele libera a propriedade do seu objeto antes que o objeto é anexado a atual auto_gcroot.

Exemplo

// msl_auto_gcroot_attach.cpp
// compile with: /clr
#include <msclr\auto_gcroot.h>

using namespace System;
using namespace msclr;

ref class ClassA {
protected:   
   String^ m_s;
public:
   ClassA( String^ s ) : m_s( s ) {
      Console::WriteLine( "in ClassA constructor:" + m_s );
   }
   ~ClassA() {
      Console::WriteLine( "in ClassA destructor:" + m_s );
   }

   virtual void PrintHello() {
      Console::WriteLine( "Hello from {0} A!", m_s );
   }
};

ref class ClassB : ClassA {
public:
   ClassB( String ^ s) : ClassA( s ) {}
   virtual void PrintHello() new {
      Console::WriteLine( "Hello from {0} B!", m_s );
   }
};

int main() {
   auto_gcroot<ClassA^> a( gcnew ClassA( "first" ) );
   a->PrintHello();
   a.attach( gcnew ClassA( "second" ) ); // attach same type
   a->PrintHello();
   ClassA^ ha = gcnew ClassA( "third" );
   a.attach( ha ); // attach raw handle
   a->PrintHello();
   auto_gcroot<ClassB^> b( gcnew ClassB("fourth") );
   b->PrintHello();
   a.attach( b ); // attach derived type
   a->PrintHello();
}
  
  
  
  
  
  

Requisitos

Arquivo de cabeçalho <msclr\auto_gcroot.h>

Namespace msclr

Consulte também

Referência

auto_gcroot::Operator =

auto_gcroot::Release

Outros recursos

auto_gcroot membros