473,480 Members | 1,799 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

what's the difference between shared_ptr and weak_ptr

When would you use which?
Jul 28 '08 #1
1 2538
On Jul 28, 5:41*pm, puzzlecracker <ironsel2...@gmail.comwrote:
When would you use which?
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
using boost::shared_ptr;
using std::cin;
using std::cout;
class a{
public:
boost::weak_ptr<aclass_member;

};
int main() {
shared_ptr<amainvar(new a);
cout<<mainvar.use_count();
mainvar->class_member = mainvar;
cout<<mainvar.use_count();
cout<<mainvar->class_member.use_count();
int a;
cin>>a;
}

in the above code weak_ptr is the must. Even when mainvar pointer goes
out of scope the reference count is still 1 since it is pointed by
class_member(shared_ptr)
to avoid this use class_member(weak_ptr) which in first does not
increase the reference count.
Jul 28 '08 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
9017
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
8
2150
by: ralpe | last post by:
Hi, I have a question regarding boost::shared_ptr. I want to let shared_ptr take care of my objects' lifetimes, so I change all my function signatures so that they accept shared_ptrs instead of...
1
2929
by: Raf256 | last post by:
Hi, how to convert following C++ code: foobar *ptr = new foobar(5); //... if (something()) ptr = NULL; if (!ptr) return no_object_is_selected_apparently; // ... if (!ptr) throw...
1
3332
by: Goran | last post by:
Hi all! I am trying to use boost smart pointers for some multithreaded work in the following way: 1. I use shared_ptr to wrap stuff in thread 1 2. I create weak_ptr-s in thread 1 and pass...
2
3798
by: Rafał Maj Raf256 | last post by:
How can I manually (other then http://www.boost.org/libs/smart_ptr/enable_shared_from_this.html) create a shared, and a weak pointer to "this"? Of course I dont want the shared / weak pointer to...
21
2648
by: Jim Langston | last post by:
I'm sure this has been asked a few times, but I'm still not sure. I want to create a function to simplify getting a reference to a CMap in a map. This is what I do now in code: ...
2
3051
by: ketan | last post by:
Hi, While reading the Boost::shared_ptr implementation, I came across this method template<class Tclass shared_ptr { ..... template<class Y> shared_ptr(shared_ptr<Yconst & r,...
9
9846
by: Christopher | last post by:
If a method is declared to return a type boost::shared_ptr<sometype>, how can the method be changed to do the equivalent of returning NULL when it was declared to return a raw pointer?
0
1264
by: raj s | last post by:
below lines are from boost"Because shared pointer implementation uses reference counting, cycles of shared_ptr instances will not be reclaimed. For example, if main() holds a shared_ptr to A, which...
0
7037
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7076
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
6732
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
6886
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5324
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4768
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
2976
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1294
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
174
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.