473,597 Members | 2,459 Online
Bytes | Software Development & Data Engineering Community
+ 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 2542
On Jul 28, 5:41*pm, puzzlecracker <ironsel2...@gm ail.comwrote:
When would you use which?
#include <iostream>
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
using boost::shared_p tr;
using std::cin;
using std::cout;
class a{
public:
boost::weak_ptr <aclass_membe r;

};
int main() {
shared_ptr<amai nvar(new a);
cout<<mainvar.u se_count();
mainvar->class_member = mainvar;
cout<<mainvar.u se_count();
cout<<mainvar->class_member.u se_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(sh ared_ptr)
to avoid this use class_member(we ak_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
9035
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 type:
8
2162
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 raw pointers. I wonder how to deal with cases where a this pointer is passed as an argument. Is it ok to write something like the following?
1
2935
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 oh_shit_no_object; // ?! else ptr->Fun();
1
3336
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 them to thread 2 3. in thread 2, I lock my weak_ptr-s and use stuff I get from there 4. in thread 1 I may destroy my shared_ptr 5. next time I want to lock my weak_ptr, I get NULL and can't use stuff
2
3803
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 cause object to be deleted when shared/weak ptr will go out of scope. -- Wymiana starych układów... na nowe układy - prawie jak walka z korupcja. Walka z wychowaniem seksualnym i erotyką - prawie jak walka z patologią.
21
2672
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: std::map<unsigned int, CMap*>::iterator ThisMapIt = World.Maps.find( ThisPlayer.Character.Map ); if ( ThisMapIt != World.Maps.end() )
2
3064
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, boost::detail::const_cast_tag):
9
9869
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
1272
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 directly or indirectly holds a shared_ptr back to A, A's use count will be 2. Destruction of the original shared_ptr will leave A dangling with a use count of 1. Use weak_ptr to "break cycles."" I was trying to create a cycle of smart pointer...
0
7971
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7893
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8276
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8381
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8040
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
3889
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
3932
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2408
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 we have to send another system
0
1243
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 can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.