smart-pointers

-> usage in smart pointers

前提是你 提交于 2019-12-01 09:24:14
问题 I have a simple smart pointer implementation shown in code snippet 1 below. And a dummy test class named Dummy in the 2nd snippet. The code snippet 3 shows how we can utilize this simple smart pointer to access the function foo(). My question is about the way we invoke the function foo() in class Dummy by using the -> operator. -> operator already returns a pointer to the raw pointer. So, I think, in order for us to be able to invoke function foo(), we need to perform a second -> operation on

How to pass shared_ptr to class with lower lifetime?

混江龙づ霸主 提交于 2019-12-01 08:47:57
I'd like to optimize my code. I have one class that have shared_ptr data member. In some method of this class I create objects that need use this member (just to get information from object pointed by shared_ptr). I know that lifetime of these created objects is lower than my main class. How to pass this pointer? I think another shared_ptrs are unnecessary (because I have warranty that object will exist). So what should get my created classes? Should they get raw pointer? Weak_ptr? Or the best solution is getting shared_ptr (and incrementing its reference counter)? What is the most standard

Is there any reason to use auto_ptr?

≯℡__Kan透↙ 提交于 2019-12-01 03:04:15
After reading Jossutis' explanation on auto_ptr from his STL book I've got a strong impression that whatever task I would try to use it in I'd 100% fail becuase of one of many auto_ptr's pitfalls. My question is: are there any real life tasks where auto_ptr is really usefull and does fit there well? Clearly, auto_ptr looses against unique_ptr . Now, in a 'strict C++03 without boost' world, I use auto_ptr quite often, most notably : For 'factory member functions' which return a dynamically allocated instance of a given type : I like the fact that using std::auto_ptr in the return type explicits

Getting a unique_ptr for a class that inherits enable_shared_from_this

♀尐吖头ヾ 提交于 2019-11-30 23:00:22
Usually I prefer returning unique_ptr from Factories. Recently I came to the problem of returning a unique_ptr for a class that inherits enable_shared_from_this . Users of this class may accidentally cause a call to shared_from_this() , though it is not owned by any shared_ptr , which results with a std::bad_weak_ptr exception (or undefined behavior until C++17, which is usually implemented as an exception). A simple version of the code: class Foo: public enable_shared_from_this<Foo> { string name; Foo(const string& _name) : name(_name) {} public: static unique_ptr<Foo> create(const string&

Is there any reason to use auto_ptr?

守給你的承諾、 提交于 2019-11-30 22:13:55
问题 After reading Jossutis' explanation on auto_ptr from his STL book I've got a strong impression that whatever task I would try to use it in I'd 100% fail becuase of one of many auto_ptr's pitfalls. My question is: are there any real life tasks where auto_ptr is really usefull and does fit there well? 回答1: Clearly, auto_ptr looses against unique_ptr . Now, in a 'strict C++03 without boost' world, I use auto_ptr quite often, most notably : For 'factory member functions' which return a

Should I use a smart pointer?

故事扮演 提交于 2019-11-30 20:35:57
I have a class like the following: class node { public: node* parent; std::list<node*> children; }; Should I use a smart pointer instead of raw pointers? Why? If yes, what kind of smart pointer? Puppy Always use a smart pointer wherever you own resources (memory, files etc). Owning them manually is extremely error prone and violates many good practices, like DRY . Which one to use depends on what ownership semantics you need. unique_ptr is best for single ownership, and shared_ptr shared ownership. As children do not own their parents, a raw parent pointer is fine. However, if the parents own

C++ shared_ptr vs. unique_ptr for resource management

只愿长相守 提交于 2019-11-30 20:30:29
I've been mulling over use of unique_ptr vs shared_ptr vs own_solution . I've discounted the latter as I'll almost certainly get it wrong, but I have a problem with both unique_ptr and shared_ptr in that neither captures precisely what I want. I want to create a resource manager which explicitly owns a resource, however I'd like the resource manager to also hand out references to the resource. If I use unique_ptr in the resource manager and hand out raw pointers there's the possibility they could escape elsewhere (though this would be against the class "contract" I suppose). If I use shared

boost smart pointer with custom deleter

烂漫一生 提交于 2019-11-30 18:41:04
问题 I can understand that boost::shared_ptr doesn't validate for NULL before calling a custom deleter function, but how can I achieve this? This will help me avoid writing dumb wrappers for fclose or any function that doesn't (rightly) specify the behaviour. My boost: #define BOOST_VERSION 104500 . This is not C++ 11 (since I use boost). The question is related to: make shared_ptr not use delete Sample code: static inline FILE* safe_fopen(const char* filename, const char* mode) { FILE* file =

c++ create shared_ptr to stack object

隐身守侯 提交于 2019-11-30 18:35:05
In my method a Player object is created like: Player player(fullName,age); My teacher gave us a piece of code with a constructor that takes a shared_ptr to a player object. //constructor of the class SomeClass(const std::shared_ptr<Socket> client, std::shared_ptr<Player> player) Lets say we want to call the constructor of SomeClass and pass the player object we created on stack. Is it ever safe/possible/good to create a shared_ptr from a stack object? To make the question more understandable lets say we have two big code projects and we want to merge them so a method from one project is called

Move ownership from std::shared_ptr to std::unique_ptr

天大地大妈咪最大 提交于 2019-11-30 18:14:52
I have a class A which has a field of type std::unique_ptr : class A { public: std::unique_ptr pointer; // class body }; And somewhere in code, I'm using few std::shared_ptr s which point to the same object. Now what I'd like to achieve is to move ownership to this std::unique_ptr in my class, so that if all shared_ptr s will be destroyed, my object will stay alive as long as this unique_ptr will stay alive. My question is - is it possible to move ownership from std::shared_ptr to std::unique_ptr and if yes, how can I do this? Logically such a scenario doesn't make sense to me. Suppose for a