shared-ptr

Can I use boost::make_shared with a private constructor?

喜欢而已 提交于 2019-11-27 06:08:45
问题 Consider the following: class DirectoryIterator; namespace detail { class FileDataProxy; class DirectoryIteratorImpl { friend class DirectoryIterator; friend class FileDataProxy; WIN32_FIND_DATAW currentData; HANDLE hFind; std::wstring root; DirectoryIteratorImpl(); explicit DirectoryIteratorImpl(const std::wstring& pathSpec); void increment(); bool equal(const DirectoryIteratorImpl& other) const; public: ~DirectoryIteratorImpl() {}; }; class FileDataProxy //Serves as a proxy to the WIN32

shared_ptr and weak_ptr differences

▼魔方 西西 提交于 2019-11-27 05:56:21
I am reading Scott Meyers "Effective C++" book. It was mentioned that there are tr1::shared_ptr and tr1::weak_ptr act like built-in pointers, but they keep track of how many tr1::shared_ptrs point to an object. This is known as reference counting. This works well in preventing resource leaks in acyclic data structures, but if two or more objects contain tr1::shared_ptrs such that a cycle is formed, the cycle may keep each other's reference count above zero, even when all external pointers to the cycle have been destroyed. That's where tr1::weak_ptrs come in. My question is how cyclic data

Create a boost::shared_ptr to an existing variable

南笙酒味 提交于 2019-11-27 05:32:42
问题 I have an existing variable, e.g. int a = 3; How can I now create a boost::shared_ptr to a ? For example: boost::shared_ptr< int > a_ptr = &a; // this doesn't work 回答1: although you should put the variable into a managed pointer on it's creation to do it from an existing pointer. int *a=new int; boost::shared_ptr<int> a_ptr(a); That said you most definitely do not want to be putting stack variables into shared_ptr BAD THINGS WILL HAPPEN If for some reason a function takes shared_ptr and you

Equality-compare std::weak_ptr

假装没事ソ 提交于 2019-11-27 05:21:52
问题 I want to compare two std::weak_ptr's or one std::weak_ptr and one std::shared_ptr for equality. What I want to know is whether the object each of the weak_ptr's/shared_ptr's point to is the same. The comparison should yield negative results not just if the addresses don't match, but also if the underlying object was deleted and then reconstructed with the same address by chance. So basically, I want this assertion to hold even if the allocator reserves the same address: auto s1 = std::make

How to release pointer from boost::shared_ptr?

流过昼夜 提交于 2019-11-27 04:28:21
Can boost::shared_ptr release the stored pointer without deleting it? I can see no release function exists in the documentation, also in the FAQ is explained why it does not provide release function, something like that the release can not be done on pointers that are not unique. My pointers are unique. How can I release my pointers ? Or which boost smart pointer class to use that will allow me releasing of the pointer ? I hope that you won't say use auto_ptr :) Martin Ba You need to use a deleter that you can request not to delete the underlying pointer. See this answer (which has been marked

Is make_shared really more efficient than new?

走远了吗. 提交于 2019-11-27 04:12:56
问题 I was experimenting with shared_ptr and make_shared from C++11 and programmed a little toy example to see what is actually happening when calling make_shared . As infrastructure I was using llvm/clang 3.0 along with the llvm std c++ library within XCode4. class Object { public: Object(const string& str) { cout << "Constructor " << str << endl; } Object() { cout << "Default constructor" << endl; } ~Object() { cout << "Destructor" << endl; } Object(const Object& rhs) { cout << "Copy constructor

Should I use shared_ptr or unique_ptr

廉价感情. 提交于 2019-11-27 03:55:06
I've been making some objects using the pimpl idiom, but I'm not sure whether to use std::shared_ptr or std::unique_ptr . I understand that std::unique_ptr is more efficient, but this isn't so much of an issue for me, as these objects are relatively heavyweight anyway so the cost of std::shared_ptr over std::unique_ptr is relatively minor. I'm currently going with std::shared_ptr just because of the extra flexibility. For example, using a std::shared_ptr allows me to store these objects in a hashmap for quick access while still being able to return copies of these objects to callers (as I

Initializing shared_ptr member variable, new vs make_shared?

两盒软妹~` 提交于 2019-11-27 03:31:41
问题 When initializing a shared_ptr member variable: // .h class Customer { public: Customer(); private: std::shared_ptr<OtherClass> something_; } // .cpp Customer(): something_(new OtherClass()) { } vs. Customer(): something_(std::make_shared<OtherClass>()) { } Is the make_shared version allowed? I always seem to see the first version, which is preferred? 回答1: The only times when make_shared is not allowed are: If you're getting a naked pointer allocated by someone else and storing it in shared

std::shared_ptr initialization: make_shared<Foo>() vs shared_ptr<T>(new Foo) [duplicate]

走远了吗. 提交于 2019-11-27 03:17:50
This question already has an answer here: Difference in make_shared and normal shared_ptr in C++ 7 answers What's the difference between: std::shared_ptr<int> p = std::shared_ptr<int>( new int ); and std::shared_ptr<int> p = std::make_shared< int >(); ? Which one should I prefer and why? P. S. Pretty sure this must have been answered already, but I can't find a similar question. Mike Seymour Both examples are rather more verbose than necessary: std::shared_ptr<int> p(new int); // or '=shared_ptr<int>(new int)' if you insist auto p = std::make_shared<int>(); // or 'std::shared_ptr<int> p' if

Where is shared_ptr?

倾然丶 夕夏残阳落幕 提交于 2019-11-27 03:05:29
I am so frustrated right now after several hours trying to find where shared_ptr is located. None of the examples I see show complete code to include the headers for shared_ptr (and working). Simply stating std , tr1 and <memory> is not helping at all! I have downloaded boosts and all but still it doesn't show up! Can someone help me by telling exactly where to find it? Thanks for letting me vent my frustrations! EDIT: I see my title has been changed. Sorry about that. So... it was also because it was not clear to me that shared_ptr is "C++ version dependant" --> that's why I did not state my