G
George3
Hello everyone,
Auto_ptr is convenient because of it saves our work and provides a
framework to auto-management life cycle to reduce potential resource
leak.
But why do we sometimes need to call release method on auto_ptr to go
back to the style of manual management? Remember, when we call release,
we need to delete the object instance manually later.
(here is a sample I modified from MSDN auto_ptr sample code)
Code:
--------------------
// auto_ptr_release.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>
using namespace std;
class Int
{
public:
Int( int i )
{
x = i;
cout << "Constructing " << ( void* )this << " Value: " << x << endl;
};
~Int( ) {
cout << "Destructing " << ( void* )this << " Value: " << x << endl;
};
int x;
};
int main( )
{
auto_ptr<Int> pi ( new Int( 5 ) );
Int* pi3 = pi.release ();
delete pi3;
}
--------------------
thanks in advance,
George
Auto_ptr is convenient because of it saves our work and provides a
framework to auto-management life cycle to reduce potential resource
leak.
But why do we sometimes need to call release method on auto_ptr to go
back to the style of manual management? Remember, when we call release,
we need to delete the object instance manually later.
(here is a sample I modified from MSDN auto_ptr sample code)
Code:
--------------------
// auto_ptr_release.cpp
// compile with: /EHsc
#include <memory>
#include <iostream>
#include <vector>
using namespace std;
class Int
{
public:
Int( int i )
{
x = i;
cout << "Constructing " << ( void* )this << " Value: " << x << endl;
};
~Int( ) {
cout << "Destructing " << ( void* )this << " Value: " << x << endl;
};
int x;
};
int main( )
{
auto_ptr<Int> pi ( new Int( 5 ) );
Int* pi3 = pi.release ();
delete pi3;
}
--------------------
thanks in advance,
George