smart-pointers

unique_ptr and polymorphism

被刻印的时光 ゝ 提交于 2019-12-09 08:23:41
问题 I have some code that currently uses raw pointers, and I want to change to smart pointers. This helps cleanup the code in various ways. Anyway, I have factory methods that return objects and its the caller's responsibility to manager them. Ownership isn't shared and so I figure unique_ptr would be suitable. The objects I return generally all derive from a single base class, Object . For example, class Object { ... }; class Number : public Object { ... }; class String : public Object { ... };

Why would I std::move an std::shared_ptr?

孤者浪人 提交于 2019-12-09 04:00:19
问题 I have been looking through the Clang source code and I found this snippet: void CompilerInstance::setInvocation( std::shared_ptr<CompilerInvocation> Value) { Invocation = std::move(Value); } Why would I want to std::move an std::shared_ptr ? Is there any point transferring ownership on a shared resource? Why wouldn't I just do this instead? void CompilerInstance::setInvocation( std::shared_ptr<CompilerInvocation> Value) { Invocation = Value; } 回答1: I think that the one thing the other

unique_ptr's assignment operator copies a deleter stored by a reference. Is it a feature or a bug?

余生长醉 提交于 2019-12-08 16:37:58
问题 Imaging the case when you have an unique_ptr with a custom deleter stored by a reference: struct CountingDeleter { void operator()(std::string *p) { ++cntr_; delete p; } unsigned long cntr_ = 0; }; int main() { CountingDeleter d1{}, d2{}; { std::unique_ptr<std::string, CountingDeleter&> p1(new std::string{"first"} , d1), p2(new std::string{"second"}, d2); p1 = std::move(p2); // does d1 = d2 under cover } std::cout << "d1 " << d1.cntr_ << "\n"; // output: d1 1 std::cout << "d2 " << d2.cntr_ <<

smart pointers, typedefs and forward declarations

丶灬走出姿态 提交于 2019-12-08 15:01:17
问题 I love using smart pointers, and have seen a bit of code which makes nice use of typedef s make make them prettier. For example: struct A { typedef boost::shared_ptr<A> pointer; }; allows me to write: A::pointer a(new A); But I have hit a minor snag in my typedef happiness :-/, forward declarations... So imagine this scenario: struct B; struct A { boost::shared_ptr<B> b_; }; struct B { boost::shared_ptr<A> a_; }; works well enough, but I'd love to clean that up a little. Unfortunately, this

Passing object by unique_ptr or by value and how to implement

妖精的绣舞 提交于 2019-12-08 08:59:21
问题 I have a case that I am not sure if I should use unique_ptr or pass Object by values. Say that I have class A which has a vector Of class B and Class C has a vector of class B as well. Every time I am adding a B Object to Vector in class C it should be Removed from vector of Class C and vice versa. When Object C is destroyed all the objects in B Vector class should be add to B vector in class A class B { public: B(); virtual ~B(); }; class A { C & c; std::vector<B> bs; public: A(C & c ): c(c)

Embedded reference count with Boost shared_ptr

て烟熏妆下的殇ゞ 提交于 2019-12-08 07:57:47
问题 I love Boost's smart_ptr features and the ability to convert to and from a shared_ptr and weak_ptr , but since the reference count is not contained in the pointed class itself, the following code does not work (and it shouldn't). A *a = new A; shared_ptr<A> aPtr1(a); { shared_ptr<A> aPtr2(a); // The reference counts of aPtr1 and aPtr2 are both 1. } // At this point, `a` is destructed by aPtr2. aPtr1->foo(); // And... SIGTERM I believe the JUCE framework has this functionality.

Allocating class member with std::shared_ptr

走远了吗. 提交于 2019-12-08 05:30:18
问题 Is my assumption, that in following example, memory referenced by b will be deallocated once instance of A goes out of scope at end of func() , correct? class A{ public: A() { b = std::shared_ptr<char>(new char[100] { 0 } ); } char* b; } void func { A a; } 回答1: No, not correct. b is of type char * and you assign to it a shared_ptr<char> . You should get a compilation error. Furthermore, the constructor is private , another compilation error. And how do you access b in func() ? It is private

Handling smart pointers in stl container

青春壹個敷衍的年華 提交于 2019-12-08 03:59:51
问题 I've a class Foo<T> which has a vector of smart pointers to Shape derived classes. I'm trying to implement an at(index) member function. Here's what I would to do intuitively: Foo<float> myfoo; std::unique_ptr<Shape<float>> shape_ptr = myfoo.at(i); shape_ptr->doSomething(param1, param2, ...); When defining the at(index) function, I'm getting a compiler error message. Note that the move constructor was defined and that the Shape base class is abstract. Below, I'm giving some code for

C++ std:.auto_ptr or std::unique_ptr (to support multiple compilers, even old C++03 compilers)?

ε祈祈猫儿з 提交于 2019-12-07 22:10:42
问题 I'm trying to update some C++ code, I'd like to move toward a more modern code (c++11), but I still need to compile the code with some older compilers (c++03 compliant), because of supported platform constraints. I know in C++11 compilers std::auto_ptr is deprecated, but because of the older compiler support, I can't just replace them with std::unique_ptr. Is there a good practice to handle this "old compiler support, but start to move to C++11"? 回答1: As you noted, std::auto_ptr<> has been

A new generic pointer any_ptr (now dumb_ptr) to make code more reusable among smart pointers

寵の児 提交于 2019-12-07 15:55:09
问题 I have been using a lot of different boost smart pointers lately, as well as normal pointers. I have noticed that as you develop you tend to realise that you have to switch pointer types and memory management mechanism because you overlooks some circular dependency or some othe small annoying thing. When this happens and you change your pointer type you have to either go and change a whole bunch of you method signatures to take the new pointer type, or at each call site you have to convert