0% found this document useful (0 votes)
16 views1 page

Smart Pointers C++

Uploaded by

Anson Antony
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views1 page

Smart Pointers C++

Uploaded by

Anson Antony
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 1

Smart Pointers C++

Ordinary Pointer explaination


1. Unique Pointer expln - unique_ptr<int> u_ptr1 = make_unique<int>(25);
reference and dereference
-> cannot be shared
-> can move ownership : unique_ptr<int> u_ptr2 = move(u_ptr1);
reference and dereference
-> After change in ownership the first one becomes null

explain constructor and destructor


class MyClass{
public:
Myclass(){
cout<<;
}
~MyClass(){
cout<<;}

unique_ptr<Myclass> u_ptr1 = make_unique<MyClass>();

------------shared pointer
can be shared
multiple owners can access
one raw pointer to multiple owners

shared_ptr<MyClass>shrd_ptr_1 = make_shared<MyClass>();

can have count of all owners


cout<<shrd_ptr_1.use_count()<<endl;
shrd_ptr_1<MyClass> shrd_ptr_2 = shrd_ptr_1
cout<<shrd_ptr_1.use_count()<<endl;

delete all ownership after the end of scope


shared pointer automatic memory deallocation

--------------
weak_ptr

if a memory location is pointed to a shared pointer it will not increase the number
of owners

preserves object in memory


locate a specific object in memory

weak_ptr<int> weakPtr1;
{
shared_ptr<int> s_ptr1 = make_shared<int>(25);
weakPtr1 = s_ptr1;
}

You might also like