smart-pointers

Using smart pointers for class members

﹥>﹥吖頭↗ 提交于 2019-12-03 00:00:12
问题 I'm having trouble understanding the usage of smart pointers as class members in C++11. I have read a lot about smart pointers and I think I do understand how unique_ptr and shared_ptr / weak_ptr work in general. What I don't understand is the real usage. It seems like everybody recommends using unique_ptr as the way to go almost all the time. But how would I implement something like this: class Device { }; class Settings { Device *device; public: Settings(Device *device) { this->device =

Should I use std::shared pointer to pass a pointer?

主宰稳场 提交于 2019-12-02 22:22:13
Suppose I have an object which is managed by an std::unique_ptr . Other parts of my code need to access this object. What is the right solution to pass the pointer? Should I just pass the plain pointer by std::unique_ptr::get or should I use and pass an std::shared_ptr instead of the std::unique_ptr at all? I have some preference for the std::unique_ptr because the owner of that pointer is actually responsible for cleanup. If I use a shared pointer, there's a chance that the object will remain alive due to a shared pointer even when it should actually be destroyed. EDIT: Unfortunately, I

Should I use C++11 emplace_back with pointers containters?

家住魔仙堡 提交于 2019-12-02 21:43:07
Having a usual Base -> Derived hierarchy, like: class Fruit { ... }; class Pear : Fruit { ... }; class Tomato : Fruit { ... }; std::vector<Fruit*> m_fruits; Has it sense (e.g: it has better performance) to use emplace_back instead of push_back? std::vector::emplace_back( new Pear() ); std::vector::emplace_back( new Tomato() ); Pointers are scalar types and therefore literal types, and so copy, move and emplace construction (from an lvalue or rvalue) are all equivalent and will usually compile to identical code (a scalar copy). push_back is clearer that you're performing a scalar copy, whereas

Why does unique_ptr have the deleter as a type parameter while shared_ptr doesn't?

烈酒焚心 提交于 2019-12-02 21:01:18
The std::unique_ptr template has two parameters: the type of the pointee, and the type of the deleter. This second parameter has a default value, so you usually just write something like std::unique_ptr<int> . The std::shared_ptr template has only one parameter though: the type of the pointee. But you can use a custom deleter with this one too, even though the deleter type is not in the class template. The usual implementation uses type erasure techniques to do this. Is there a reason the same idea was not used for std::unique_ptr ? Part of the reason is that shared_ptr needs an explicit

Idiomatic use of std::auto_ptr or only use shared_ptr?

╄→尐↘猪︶ㄣ 提交于 2019-12-02 20:36:42
Now that shared_ptr is in tr1, what do you think should happen to the use of std::auto_ptr ? They both have different use cases, but all use cases of auto_ptr can be solved with shared_ptr , too. Will you abandon auto_ptr or continue to use it in cases where you want to express explicitly that only one class has ownership at any given point? My take is that using auto_ptr can add clarity to code, precisely by adding nuance and an indication of the design of the code, but on the other hand, it add yet another subtle issue when training new programmers: they need to understand smart pointers and

Lock-free Reference counting and C++ smart pointers

十年热恋 提交于 2019-12-02 20:17:58
In general, most widely known implementations of reference-counting smart ptr classes in C++, including the standard std::shared_ptr , use atomic reference counting, but do not provide atomic access to the same smart ptr instance. In other words, multiple threads may safely operate on separate shared_ptr instances which point to the same shared object, but multiple threads cannot safely read/write instances of the same shared_ptr instance without providing some kind of synchronization such as a mutex or whatever. An atomic version of a shared_ptr called " atomic_shared_ptr " has been proposed

What is boost's shared_ptr(shared_ptr<Y> const & r, T * p) used for?

我与影子孤独终老i 提交于 2019-12-02 19:10:45
boost::shared_ptr has an unusual constructor template<class Y> shared_ptr(shared_ptr<Y> const & r, T * p); and I am a little puzzled as to what this would be useful for. Basically it shares ownership with r , but .get() will return p . not r.get() ! This means you can do something like this: int main() { boost::shared_ptr<int> x(new int); boost::shared_ptr<int> y(x, new int); std::cout << x.get() << std::endl; std::cout << y.get() << std::endl; std::cout << x.use_count() << std::endl; std::cout << y.use_count() << std::endl; } And you will get this: 0x8c66008 0x8c66030 2 2 Note that the

Vector of shared pointers , memory problems after clearing the vector

为君一笑 提交于 2019-12-02 19:03:29
I realized that after calling vector.clear() which hold shared pointers, the destructors of the object which own by shared_ptr is not being released. Code example can be seen below . Even vector.clear() being called, destructor called after shared pointer goes beyond the scope.My question is - do I have to delete all smart pointers inside the vector manually by resetting them? Is there any easier way that you can advice ? Output : constructor I am here destructor Code: #include <vector> #include <iostream> #include <memory> using namespace std; class A { public: A(){cout << "constructor" <<

How do I pass smart pointers into functions?

ぃ、小莉子 提交于 2019-12-02 17:15:32
When passing objects into functions, do the same rules apply to smart pointers as to other objects that contain dynamic memory? When I pass, for example, a std::vector<std::string> into a function I always consider the following options: I'm going to change the state of the vector object, but I do not want those changes reflected after the function has finished, AKA make a copy. void function(std::vector<std::string> vec); I'm going to change the state of the vector object, and I do want those changes reflected after the function has finished, AKA make a reference. void function(std::vector

Using smart pointers for class members

只愿长相守 提交于 2019-12-02 13:46:49
I'm having trouble understanding the usage of smart pointers as class members in C++11. I have read a lot about smart pointers and I think I do understand how unique_ptr and shared_ptr / weak_ptr work in general. What I don't understand is the real usage. It seems like everybody recommends using unique_ptr as the way to go almost all the time. But how would I implement something like this: class Device { }; class Settings { Device *device; public: Settings(Device *device) { this->device = device; } Device *getDevice() { return device; } }; int main() { Device *device = new Device(); Settings