weak-ptr Questions
3
Solved
I have two classes A and B, where B is a subclass of A. I need both classes to use std::enable_shared_from_this.
I tried this:
#include <memory>
#include <iostream>
#include <vecto...
Daggerboard asked 17/3, 2020 at 19:55
10
Solved
If I understand correctly, a weak_ptr doesn't increment the reference count of the managed object, therefore it doesn't represent ownership. It simply lets you access an object, the lifetime of whi...
Depone asked 15/3, 2015 at 9:58
2
Solved
So I have code that uses std::weak_ptr and maintains them in an std::set, and that works just fine -- and has worked for the last 5 or 7 years. Recently I thought I'd fiddle with using them in an s...
4
Solved
I read about shared pointers and understood how to use. But I never understood the cyclic dependency problem with shared pointers and how weak pointers are going to fix those issues. Can any one pl...
Cartage asked 5/3, 2014 at 0:8
15
Solved
I started studying smart pointers of C++11 and I don't see any useful use of std::weak_ptr. Can someone tell me when std::weak_ptr is useful/necessary?
Churchill asked 19/8, 2012 at 23:0
2
Solved
I'd like to reset a shared_ptr without deleting its object and let weak_ptr of it loses a reference to it. However, shared_ptr doesn't have release() member function for reasons, so I can't directl...
Uncinate asked 3/2, 2022 at 23:47
5
std::shared_ptr<int> g_s = std::make_shared<int>(1);
void f1()
{
std::shared_ptr<int>l_s1 = g_s; // read g_s
}
void f2()
{
std::shared_ptr<int> l_s2 = std::make_shared<...
Tusker asked 20/12, 2013 at 14:1
2
I'm learning smart pointers and shared_from_this. In Class Inheritance Relations, it will be very hard to understand.
I have two base classes CA and CB, they are derived from enable_shared_from_th...
Triplenerved asked 31/8, 2019 at 13:23
4
Solved
I have a set like this: set<weak_ptr<Node>, owner_less<weak_ptr<Node> > > setName;
It works fine. But I would like to change it to an unordered set. However, I get about si...
Lactam asked 4/12, 2012 at 3:30
5
Solved
Here is the code:
struct lex_compare {
bool operator() (const weak_ptr<int> &lhs, const weak_ptr<int> &rhs)const {
return *lhs.lock() < *rhs.lock();
}
};
int main(){
se...
Gynecic asked 19/9, 2015 at 13:23
2
Solved
The final example at page 137 of Effective Modern C++ draws the scenario of a data structure with objects A, B, and C in it, connected to each other via std::shared_ptr in the following way:
std::...
Avoidance asked 19/11, 2020 at 8:40
3
Solved
I'm currently designing a object structure for a game, and the most natural organization in my case became a tree. Being a great fan of smart pointers I use shared_ptr's exclusively. However, in th...
Offcolor asked 30/4, 2010 at 22:40
1
The below code produce a warning when running with thread sanitizer on macOS. I can't see where the race is. The control block of shared_ptr and weak_ptr is thread safe, and pushing and popping fro...
Sharecropper asked 18/11, 2019 at 11:42
3
Solved
I am developing a library with some classes, let's call them C1, C2 and ... Cn. Each of these classes realize some interfaces, i.e. I1, I2, ... Im. (n > m). The relationship between objects in the ...
Higgs asked 23/10, 2019 at 8:25
2
Solved
How can I use std::weak_ptr as key for a std::map as shown in the following code?
#include <map>
#include <memory>
int main()
{
std::map< std::weak_ptr<int>, bool > myMap...
Schulz asked 13/10, 2012 at 18:17
2
Solved
I'm having trouble using shared_ptr and weak_ptr along with enable_shared_from_this.
When I google the symptoms of what I'm seeing, everybody suggests "you cannot use shared_from_this() when there...
Insignia asked 16/7, 2019 at 19:33
3
Solved
I have member function (method) which uses
std::enable_shared_from_this::weak_from_this()
In short: weak_from_this returns weak_ptr to this. One caveat is it can't be used from constructor.
If...
Repeated asked 8/4, 2019 at 14:53
2
Solved
Typically if you are using a std::shared_ptr to point to an object and you want to create another pointer to that object that does not share ownership you would create a std::weak_ptr.
// Create a...
Broody asked 6/3, 2019 at 17:4
4
Solved
#include <memory>
#include <iostream>
struct A : public std::enable_shared_from_this<A>
{
~A()
{
auto this_ptr = shared_from_this(); // std::bad_weak_ptr exception here.
std...
Subcutaneous asked 5/2, 2015 at 8:5
1
Solved
I know you can't use shared_from_this from within a constructor. But is it ok to use the new weak_from_this from within a constructor? According to cppreference:
This is a copy of the the privat...
Haslam asked 28/5, 2018 at 0:54
1
Solved
I am currently designing an API and I am not sure whether my functions should take shared_ptr or weak_ptr. There are widgets that contain viewers. The viewers have a function add_painter which adds...
Corncob asked 31/1, 2018 at 8:6
4
Consider the following code:
#include <iostream>
#include <memory>
using namespace std;
class T;
std::weak_ptr<T> wptr;
class T
{
public:
T() { }
~T() {
std::cout << ...
Munificent asked 25/1, 2017 at 12:20
2
Given this class which is enable_shared_from_this:
class connection : public std::enable_shared_from_this<connection>
{
//...
};
Suppose I create two instances of std::shared_ptr from the...
Herren asked 13/11, 2017 at 4:46
2
Solved
I was trying with the cyclic references for boost::shared_ptr, and devised following sample:
class A{ // Trivial class
public:
i32 i;
A(){}
A(i32 a):i(a){}
~A(){
cout<<"~A : "<<i&...
Raised asked 9/9, 2012 at 10:14
1
I have a std::shared_ptr with a custom deleter and in that deleter, I would like to take a temporary copy of the original std::shared_ptr. Expressed in code form:
struct Foo : public std::enable_s...
Benfield asked 9/10, 2017 at 12:53
1 Next >
© 2022 - 2025 — McMap. All rights reserved.