shared-ptr

Difference between `const shared_ptr<T>` and `shared_ptr<const T>`?

天涯浪子 提交于 2019-11-28 15:12:12
I'm writing an accessor method for a shared pointer in C++ that goes something like this: class Foo { public: return_type getBar() const { return m_bar; } private: boost::shared_ptr<Bar> m_bar; } So to support the const-ness of getBar() the return type should be a boost::shared_ptr that prevents modification of the Bar it points to. My guess is that shared_ptr<const Bar> is the type I want to return to do that, whereas const shared_ptr<Bar> would prevent reassignment of the pointer itself to point to a different Bar but allow modification of the Bar that it points to... However, I'm not sure.

static_cast with boost::shared_ptr?

心已入冬 提交于 2019-11-28 14:55:37
问题 What is the equivalent of a static_cast with boost::shared_ptr ? In other words, how do I have to rewrite the following Base* b = new Derived(); Derived* d = static_cast<Derived*>(b); when using shared_ptr ? boost::shared_ptr<Base> b(new Derived()); boost::shared_ptr<Derived> d = ??? 回答1: Use boost::static_pointer_cast : boost::shared_ptr<Base> b(new Derived()); boost::shared_ptr<Derived> d = boost::static_pointer_cast<Derived>(b); 回答2: There are three cast operators for smart pointers:

Extracting a raw pointer from a shared_ptr

喜你入骨 提交于 2019-11-28 12:31:44
问题 Is it possible to extract a raw pointer from a std::shared_ptr or std::tr1::shared_ptr object? The intent is to tell the smart pointer object that I don't want it to manage the lifetime of the object anymore. The context is that I have an API that takes a raw pointer from users and does some processing on the object. To make things easier to manage API creates a shared_ptr out of this raw pointer. Now, the user might ask for the object to be returned. In that case, when giving the processed

enable_shared_from_this (c++0x): what am I doing wrong?

淺唱寂寞╮ 提交于 2019-11-28 12:07:49
I'm just toying around with the smart pointers in the upcoming new c++ standard. However I fail to grasp the usage of the shared_from_this function. Here is what I have: #include <iostream> #include <memory> class CVerboseBornAndDie2 : public std::enable_shared_from_this<CVerboseBornAndDie2> { public: std::string m_Name; CVerboseBornAndDie2(std::string name) : m_Name(name) { std::cout << m_Name << " (" << this << ") is born!" << std::endl; } virtual ~CVerboseBornAndDie2() { std::cout << m_Name << " (" << this << ") is dying!" << std::endl; } }; int main(){ CVerboseBornAndDie2* vbad = new

Boost::process output blank lines

ⅰ亾dé卋堺 提交于 2019-11-28 10:23:27
问题 I am developing an application where I need to launch and stop a variety of different executables depending on user input. I would like my "core" program to run as normal whilst these executables run, i.e not wait for their termination which could theoretically be infinte. As well as this I need to be able to receive std_out and send std_in to these executables. At the moment I have a set up where I have a process manager class: class ProcessManager { private: std::vector<patchProcess>

Initializing shared_ptr member variable, new vs make_shared?

旧巷老猫 提交于 2019-11-28 10:21:10
When initializing a shared_ptr member variable: // .h class Customer { public: Customer(); private: std::shared_ptr<OtherClass> something_; } // .cpp Customer(): something_(new OtherClass()) { } vs. Customer(): something_(std::make_shared<OtherClass>()) { } Is the make_shared version allowed? I always seem to see the first version, which is preferred? The only times when make_shared is not allowed are: If you're getting a naked pointer allocated by someone else and storing it in shared_ptr . This is often the case when interfacing with C APIs. If the constructor you want to call is not public

Errors in std::make_shared() when trying to make shared_ptr?

不打扰是莪最后的温柔 提交于 2019-11-28 07:53:18
问题 (Using Visual Studio 2010) I'm trying to create a shared_ptr of an existing class in my project (class was written a decade before std::shared_ptr existed). This class takes a non-const pointer to another object, it's empty parameter constructor is private. class Foobar { public: Foobar(Baz* rBaz); private: Foobar(); } When I try to create a shared_ptr to it, things don't go well: Baz* myBaz = new Baz(); std::shared_ptr<Foobar> sharedFoo = std::make_shared<Foobar>(new Foobar(myBaz)); On

intrusive_ptr in c++11

你说的曾经没有我的故事 提交于 2019-11-28 07:14:14
Does C++11 have something equivalent to boost::intrusive_ptr ? My problem is that I have a C-style interface over my C++ code. Both sides of the interface can use C++, but exposing the C interface is required for compatibility reasons. I cannot use std::shared_ptr because I have to manage the object through two (or more) smart pointers. I am unable to figure out a solution with something like boost::intrusive_ptr . Does c++11 have something equivalent to boost::intrusive_ptr? No. It does have std::make_shared which means std::shared_ptr is almost (see note below) as efficient as an intrusive

Error: expected type-specifier before 'ClassName'

一个人想着一个人 提交于 2019-11-28 06:43:54
shared_ptr<Shape> circle(new Circle(Vec2f(0, 0), 0.1, Vec3f(1, 0, 0))); shared_ptr<Shape> rect(new Rect2f(Vec2f(0, 0), 5.0f, 5.0f, 0, Vec3f(1.0f, 1.0f, 0)) ); I'm trying to understand why the above won't compile. For Whatever reason, when I try to create an instance of Rect2f (which DOES inherit from the Shape class specified the shared_ptr template argument, just like Circle ), I get the following errors : error: expected type-specifier before 'Rect2f' error: expected ')' before 'Rect2f' Everything about the Circle shared_ptr is perfectly fine. There are no problems with it; it's only the

boost::weak_ptr<T>.lock() Crashes with a SIGSEGV Segmentation Fault

霸气de小男生 提交于 2019-11-28 06:17:30
问题 (EDIT) Environment: plee@sos-build:/usr/local/include/boost$ lsb_release -a No LSB modules are available. Distributor ID: Ubuntu Description: Ubuntu 11.10 Release: 11.10 Codename: oneiric plee@sos-build:/usr/local/include/boost$ uname -a Linux sos-build 3.0.0-12-generic #20-Ubuntu SMP Fri Oct 7 14:56:25 UTC 2011 x86_64 x86_64 x86_64 GNU/Linux plee@sos-build:/usr/local/include/boost$ plee@sos-build:/usr/local/include/boost$ cat version.hpp // BOOST_LIB_VERSION must be defined to be the same as