auto_gcroot::reset
The new home for Visual Studio documentation is Visual Studio 2017 Documentation on docs.microsoft.com.
The latest version of this topic can be found at auto_gcroot::reset.
Destroy the current owned object and optionally take possession of a new object.
Syntax
void reset(
_element_type _new_ptr = nullptr
);
Parameters
_new_ptr
(Optional) The new object.
Example
// msl_auto_gcroot_reset.cpp
// compile with: /clr
#include <msclr\auto_gcroot.h>
using namespace System;
using namespace msclr;
ref class ClassA {
String^ m_s;
public:
ClassA( String^ s ) : m_s( s ) {
Console::WriteLine( "ClassA constructor: " + m_s );
}
~ClassA() {
Console::WriteLine( "ClassA destructor: " + m_s );
}
void PrintHello() {
Console::WriteLine( "Hello from {0} A!", m_s );
}
};
int main()
{
auto_gcroot<ClassA^> agc1 = gcnew ClassA( "first" );
agc1->PrintHello();
ClassA^ ha = gcnew ClassA( "second" );
agc1.reset( ha ); // release first object, reference second
agc1->PrintHello();
agc1.reset(); // release second object, set to nullptr
Console::WriteLine( "done" );
}
ClassA constructor: first
Hello from first A!
ClassA constructor: second
ClassA destructor: first
Hello from second A!
ClassA destructor: second
done
Requirements
Header file <msclr\auto_gcroot.h>
Namespace msclr
See Also
auto_gcroot Members
auto_gcroot::release
auto_gcroot::attach