auto_gcroot::attach
Attach auto_gcroot to an object.
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
);
Parameters
- _right
The object to attach, or an auto_gcroot containing the object to attach.
Return Value
The current auto_gcroot.
Remarks
If _right is an auto_gcroot, it releases ownership of its object before the object is attached to the current auto_gcroot.
Example
// 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();
}
in ClassA constructor:first Hello from first A! in ClassA constructor:second in ClassA destructor:first Hello from second A! in ClassA constructor:third in ClassA destructor:second Hello from third A! in ClassA constructor:fourth Hello from fourth B! in ClassA destructor:third Hello from fourth A! in ClassA destructor:fourth
Requirements
Header file <msclr\auto_gcroot.h>
Namespace msclr