Tag Archives: auto_ptr

C++ source code that demos auto_ptr and auto_ptr.release()

Project is auto_ptr
//#include
#include
using namespace std;

class MyClass {
public:
MyClass() {} // nothing
~MyClass() {} // nothing
void myFunc() { cout<<"hi"< ptr1(new MyClass), ptr2;

ptr2 = ptr1;
ptr2->myFunc();

ptr1->myFunc();
MyClass* ptr = ptr2.get();

ptr->myFunc();

auto_ptr p1 (new int);
*p1.get()=10;

auto_ptr p2 (p1); //transfers ownership from p1 to p2

cout << "p2 points to " << *p2 << "\n"; // (p1 is now null-pointer auto_ptr where next line will cause crash //cout<<"p1 points to "<<*p1< p;

p.reset (new int);
*p=5;
cout << *p << endl; p.reset (new int); cout << "after 2nd reset "<<*p << endl; //this will print junk *p=10; cout << *p << endl; return 0; } Project auto_ptr_release: // auto_ptr::release example #include
#include
using namespace std;

int main () {
auto_ptr auto_pointer (new int);
int * manual_pointer;

*auto_pointer=10;
cout<<"auto_pointer="<<*auto_pointer< NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!

A C++ smart pointer including auto_ptr and shared_ptr and difference from Boost

C++ smart pointers including auto_ptr and shared_ptr

Note this is not complete!!

Smart pointers are used to prevent dangling pointers.
This is a demo of an auto_ptr:
#include
using namespace std;

class MyClass {
public:
MyClass() {} // nothing
~MyClass() {} // nothing
void myFunc() {} // nothing
};

int main() {
auto_ptr ptr1(new MyClass), ptr2;

ptr2 = ptr1;
ptr2->myFunc();

MyClass* ptr = ptr2.get();

ptr->myFunc();

return 0;
}
}
Auto_ptr has ownership but when you assign to another auto pointer, the assign auto pointer will lose ownership. The right hand side can also get modified.
Need to complete both types including shared_ptr within Boost. Need to know pros and cons of each.

NOTE I now post my TRADING ALERTS into my personal FACEBOOK ACCOUNT and TWITTER. Don't worry as I don't post stupid cat videos or what I eat!