smart-pointers

Copy constructor with smart pointer

三世轮回 提交于 2019-11-30 05:23:50
问题 I have a class with one std::unique_ptr as class member. I was wondering, how to correctly define the copy constructor, since I'm getting the following compiler error message: error C2248: std::unique_ptr<_Ty>::unique_ptr : cannot access private member declared in class 'std::unique_ptr<_Ty> . My class design looks something like: template <typename T> class Foo{ public: Foo(){}; Foo( Bar<T> *, int ); Foo( const Foo<T> & ); ~Foo(){}; void swap( Foo<T> & ); Foo<T> operator = ( Foo<T> );

C++ shared_ptr vs. unique_ptr for resource management

亡梦爱人 提交于 2019-11-30 05:03:05
问题 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

Smart pointer wrapping penalty. Memoization with std::map

一笑奈何 提交于 2019-11-30 04:56:39
问题 I am currently in the middle of a project where performance is of vital importance. Following are some of the questions I had regarding this issue. Question1 My project involves plenty of boost::shared_ptr .I know creating shared pointers on the run using boost::make_shared is slow since there is a lot of overhead as it needs to track references.I wanted to know what if the boost shared pointers are already created then would these two statements have the same performance or would one be

Why shouldn't you use references to smart pointers?

与世无争的帅哥 提交于 2019-11-30 04:37:51
I recall reading somewhere that using references to smart pointers can cause memory corruption. Is this simply because of using the reference of the smart pointer after its been destroyed? Or does the reference counting get messed up? Thanks for clarifying Assuming you are talking about shared_ptr here... Is this simply because of using the reference of the smart pointer after its been destroyed? This is a good answer. You may not know absolutely the lifetime of the pointer your reference refers too. To get around this, you'd want to look into boost::weak_ptr. It doesn't participate in

Should I use a smart pointer?

≯℡__Kan透↙ 提交于 2019-11-30 04:30:28
问题 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? 回答1: 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

unique_ptr<T> lambda custom deleter for array specialization [duplicate]

蹲街弑〆低调 提交于 2019-11-30 04:24:57
This question already has an answer here: How do I use a custom deleter with a std::unique_ptr member? 6 answers I recently started porting lots of my existing C++ application code to over to C++11 and now that I am converting to the new smart pointers std::unique_ptr and std::shared_ptr , I have a specific question about custom deleters. I want to add a lambda logger to see where my deletes are being called but I cannot get the array specialization version to compile. Advice would be very much appreciated. I have been searching in vain for an example of a custom deleter for array

getting a normal ptr from shared_ptr?

我们两清 提交于 2019-11-30 04:11:28
I have something like shared_ptr<Type> t(makeSomething(), mem_fun(&Type::deleteMe)) I now need to call C styled function that requires a pointer to Type . How do I get it from shared_ptr ? Use the get() method: boost::shared_ptr<foo> foo_ptr(new foo()); foo *raw_foo = foo_ptr.get(); c_library_function(raw_foo); Make sure that your shared_ptr doesn't go out of scope before the library function is done with it -- otherwise badness could result, since the library may try to do something with the pointer after it's been deleted. Be especially careful if the library function maintains a copy of the

What's the difference between BSTR and _bstr_t?

和自甴很熟 提交于 2019-11-30 01:49:31
Can anyone explain the difference between the types mentioned above and some sample usage to clearly explain the difference between the two? Any help would be highly appreciated! Note: this question is a spin-off from this other question BSTR is the string data type used with COM. _bstr_t is a wrapper class that works like a smart pointer, so it will free the allocated memory when the variable is destroyed or goes out of scope. _bstr_t also has reference counting, which increases every time you pass the _bstr_t variable by value (avoiding unnecessary copy) and decrement when it is no longer

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

限于喜欢 提交于 2019-11-30 01:17:50
问题 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

How to pass deleter to make_shared?

穿精又带淫゛_ 提交于 2019-11-30 00:26:28
Since C++11, because of several reasons, developers tend to use smart pointer classes for dynamic lifetime objects. And with those new smart pointer classes, standards, even suggest to not use operators like new instead they suggest to use make_shared or make_unique to avoid some error prone. If we like to use a smart pointer class, like shared_ptr , we can construct one like, shared_ptr<int> p(new int(12)); Also we would like to pass a custom deleter to smart pointer classes, shared_ptr<int> p(new int(12), deleter); On the other hand, if we like to use make_shared to allocate, for ex. int ,