shared-ptr

Call default copy constructor from within overloaded copy constructor

给你一囗甜甜゛ 提交于 2020-01-13 07:55:45
问题 I need to write a copy constructor that deep copies the contents of a std::shared_ptr . However, there are a bunch of variable int a, b, c, d, e; also defined in the class. Is there a way to generate the default copy constructor code (or call the default copy constructor) inside my new overloaded one. Here is a code snippet with a comment that hopefully clarifies the issue. class Foo { public: Foo() {} Foo(Foo const & other); ... private: int a, b, c, d, e; std::shared_ptr<Bla> p; }; Foo::Foo

Initializing boost::asio sockets after constructor

元气小坏坏 提交于 2020-01-13 05:31:12
问题 [Update:] The answer for whoever is interested, is simply wrapping the io_service member var in boost::ref (boost::ref(io_service_)) I am experimenting with the udp server example from boost asio libraries, to see if I can initialize the socket somewhere other than in the constructor. The constructor proposed in the example is the following: class server { public: server(boost::asio::io_service& io_service, short port) : io_service_(io_service), socket_(io_service, udp::endpoint(udp::v4(),

Boost shared_from_this and destructor

眉间皱痕 提交于 2020-01-12 10:08:46
问题 I found that it is not allowed to call shared_from_this in the destructor from a class: https://svn.boost.org/trac/boost/ticket/147 This behavior is by design. Since the destructor will destroy the object, it is not safe to create a shared_ptr to it as it will become dangling once the destructor ends. I understand the argument, but what if I need a "shared_from_this" pointer for cleaning up references ( not for sharing owner ship). Here is an example where I'm not using shared_ptr: class A{

What is boost's shared_ptr(shared_ptr<Y> const & r, T * p) used for?

心已入冬 提交于 2020-01-11 15:49:26
问题 boost::shared_ptr has an unusual constructor template<class Y> shared_ptr(shared_ptr<Y> const & r, T * p); and I am a little puzzled as to what this would be useful for. Basically it shares ownership with r , but .get() will return p . not r.get() ! This means you can do something like this: int main() { boost::shared_ptr<int> x(new int); boost::shared_ptr<int> y(x, new int); std::cout << x.get() << std::endl; std::cout << y.get() << std::endl; std::cout << x.use_count() << std::endl; std:

What is boost's shared_ptr(shared_ptr<Y> const & r, T * p) used for?

落花浮王杯 提交于 2020-01-11 15:49:25
问题 boost::shared_ptr has an unusual constructor template<class Y> shared_ptr(shared_ptr<Y> const & r, T * p); and I am a little puzzled as to what this would be useful for. Basically it shares ownership with r , but .get() will return p . not r.get() ! This means you can do something like this: int main() { boost::shared_ptr<int> x(new int); boost::shared_ptr<int> y(x, new int); std::cout << x.get() << std::endl; std::cout << y.get() << std::endl; std::cout << x.use_count() << std::endl; std:

C++11 atomics and intrusive shared pointer reference count

被刻印的时光 ゝ 提交于 2020-01-10 07:38:07
问题 I am writing intrusive shared pointer and I am using C++11 <atomic> facilities for reference counter. Here are the relevant fragments of my code: //... mutable std::atomic<unsigned> count; //... void SharedObject::addReference() const { std::atomic_fetch_add_explicit (&count, 1u, std::memory_order_consume); } void SharedObject::removeReference() const { bool destroy; destroy = std::atomic_fetch_sub_explicit (&count, 1u, std::memory_order_consume) == 1; if (destroy) delete this; } I have

boost shared_from_this and multiple inheritance

≡放荡痞女 提交于 2020-01-09 19:47:48
问题 I am currently having some troubles when using boost enable_shared_from_this and multiple inheritance. The scenario can be described as follows: Class A implements some functionality and should inherit from enable_shared_from_this Class B implements another functionality and should inherit from enable_shared_from_this Class D inherits functionalities from A and B ( class D : public A, public B {} ) When using some class B functionality from class D I got an exception ( bad_weak_ptr ) To

boost shared_from_this and multiple inheritance

心已入冬 提交于 2020-01-09 19:47:35
问题 I am currently having some troubles when using boost enable_shared_from_this and multiple inheritance. The scenario can be described as follows: Class A implements some functionality and should inherit from enable_shared_from_this Class B implements another functionality and should inherit from enable_shared_from_this Class D inherits functionalities from A and B ( class D : public A, public B {} ) When using some class B functionality from class D I got an exception ( bad_weak_ptr ) To

How to release pointer from boost::shared_ptr?

筅森魡賤 提交于 2020-01-09 02:15:06
问题 Can boost::shared_ptr release the stored pointer without deleting it? I can see no release function exists in the documentation, also in the FAQ is explained why it does not provide release function, something like that the release can not be done on pointers that are not unique. My pointers are unique. How can I release my pointers ? Or which boost smart pointer class to use that will allow me releasing of the pointer ? I hope that you won't say use auto_ptr :) 回答1: You need to use a deleter

make_shared with custom new operator

你。 提交于 2020-01-06 02:30:33
问题 This is probably a duplicate, but I cannot find the solution anywhere. I have source code like this: struct Blob{ //... static void *operator new(size_t size_reported, size_t size) { return ::operator new(size); } }; I use it like this: std::shared_ptr<Blob> blob; // ... size_t size = calcSize(); // it returns say 231 Blob *p = new(size) Blob(); blob.reset(p); Can I change the code somehow so I can use std::make_shared or std::allocate_shared so I have single allocation instead of two