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

Weak Pointers

Weak pointers are used to locate an object in memory without increasing its reference count or keeping it alive. The code demonstrates creating a weak pointer to a shared pointer within a local scope, so the shared pointer is destroyed but the weak pointer can still access the object.

Uploaded by

markoskadiu7
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)
3 views1 page

Weak Pointers

Weak pointers are used to locate an object in memory without increasing its reference count or keeping it alive. The code demonstrates creating a weak pointer to a shared pointer within a local scope, so the shared pointer is destroyed but the weak pointer can still access the object.

Uploaded by

markoskadiu7
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

// Weak pointers

// we use weak pointers to locate a specific obj in memory


//it wont keep the value and wont raise the number of owners
#include <iostream>
#include <memory>
using namespace std;
class MyClass
{
public:
MyClass() {
cout << "Constructor is invoked" << endl;
}
~MyClass() {
cout << "Destructor is invoked" << endl;
}

};
int main() {

weak_ptr<int>wePtr1;
{
shared_ptr<int>shPtr1 = make_shared<int>(27);
wePtr1 = shPtr1;
}

system("pause>nul");
}

You might also like