shared-ptr

Is boost shared_ptr <XXX> thread safe?

寵の児 提交于 2019-11-26 12:59:06
问题 I have a question about boost::shared_ptr<T> . There are lots of thread. using namespace boost; class CResource { // xxxxxx } class CResourceBase { public: void SetResource(shared_ptr<CResource> res) { m_Res = res; } shared_ptr<CResource> GetResource() { return m_Res; } private: shared_ptr<CResource> m_Res; } CResourceBase base; //---------------------------------------------- // Thread_A: while (true) { //... shared_ptr<CResource> nowResource = base.GetResource(); nowResource.doSomeThing();

Where is shared_ptr?

荒凉一梦 提交于 2019-11-26 12:37:37
问题 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

Passing shared_ptr<Derived> as shared_ptr<Base>

喜你入骨 提交于 2019-11-26 11:55:14
问题 What is the best method to go about passing a shared_ptr of a derived type to a function that takes a shared_ptr of a base type? I generally pass shared_ptr s by reference to avoid a needless copy: int foo(const shared_ptr<bar>& ptr); but this doesn\'t work if I try to do something like int foo(const shared_ptr<Base>& ptr); ... shared_ptr<Derived> bar = make_shared<Derived>(); foo(bar); I could use foo(dynamic_pointer_cast<Base, Derived>(bar)); but this seems sub-optimal for two reasons: A

Should I pass a shared_ptr by reference? [duplicate]

做~自己de王妃 提交于 2019-11-26 11:55:05
问题 This question already has an answer here: Should we pass a shared_ptr by reference or by value? 8 answers What are the best practices for passing a shared_ptr? Currently I pass shared_ptr function arguments like so: void function1( shared_ptr<TYPE>& value ); 回答1: In controlled circumstances you can pass the shared pointer by constant reference . Be sure that nobody is concurrently deleting the object, though this shouldn't be too hard if you're careful about to whom you give references. In

Should I use shared_ptr or unique_ptr

北慕城南 提交于 2019-11-26 10:57:10
问题 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

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

你离开我真会死。 提交于 2019-11-26 10:21:49
问题 This question already has an answer here: Difference in make_shared and normal shared_ptr in C++ 8 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. 回答1: Both examples are rather more verbose than necessary: std::shared_ptr<int> p(new int); // or '=shared_ptr

Boost async_* functions and shared_ptr&#39;s

左心房为你撑大大i 提交于 2019-11-26 08:25:32
问题 I frequently see this pattern in code, binding shared_from_this as the first parameter to a member function and dispatching the result using an async_* function. Here\'s an example from another question: void Connection::Receive() { boost::asio::async_read(socket_,boost::asio::buffer(this->read_buffer_), boost::bind(&Connection::handle_Receive, shared_from_this(), boost::asio::placeholders::error, boost::asio::placeholders::bytes_transferred)); } The only reason to use shared_from_this()

Passing shared pointers as arguments

妖精的绣舞 提交于 2019-11-26 07:54:43
问题 If I declare an object wrapped in a shared pointer: std::shared_ptr<myClass> myClassObject(new myClass()); then I wanted to pass it as an argument to a method: DoSomething(myClassObject); //the called method void DoSomething(std::shared_ptr<myClass> arg1) { arg1->someField = 4; } Does the above simply increment the shared_pt\'s reference count and everything is cool? Or does it leave a dangling pointer? Are you still supposed to do this?: DoSomething(myClassObject.Get()); void DoSomething(std

C++ - passing references to std::shared_ptr or boost::shared_ptr

有些话、适合烂在心里 提交于 2019-11-26 07:53:43
问题 If I have a function that needs to work with a shared_ptr , wouldn\'t it be more efficient to pass it a reference to it (so to avoid copying the shared_ptr object)? What are the possible bad side effects? I envision two possible cases: 1) inside the function a copy is made of the argument, like in ClassA::take_copy_of_sp(boost::shared_ptr<foo> &sp) { ... m_sp_member=sp; //This will copy the object, incrementing refcount ... } 2) inside the function the argument is only used, like in Class:

Should we pass a shared_ptr by reference or by value?

不问归期 提交于 2019-11-26 05:58:56
问题 When a function takes a shared_ptr (from boost or C++11 STL), are you passing it: by const reference: void foo(const shared_ptr<T>& p) or by value: void foo(shared_ptr<T> p) ? I would prefer the first method because I suspect it would be faster. But is this really worth it or are there any additional issues? Could you please give the reasons for your choice or if the case, why you think it does not matter. 回答1: This question has been discussed and answered by Scott, Andrei and Herb during Ask