nt main() { // p1 is an auto_ptr of type A // returns the memory address of p1 cout << p1.get() << endl; // copy constructor called, this makes p1 empty. auto_ptr p2(p1); p2 -> show(); // p1 is empty now cout << p1.get() << endl; // p1 gets copied in p2 cout<< p2.get() << endl; }
two pointers of same type can’t point to the same resource at the same time.
As shown in below program, copying or assigning of pointers changes the ownership i.e. source pointer has to give ownership to the destination pointer.
|
Friday, 22 June 2018
auto_ptr, unique_ptr, shared_ptr and weak_ptr
auto_ptr is a smart pointer that manages an object obtained via new expression and deletes that object when auto_ptr itself is destroyed.
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment