shared-ptr

Why does unique_ptr take two template parameters when shared_ptr only takes one?

不羁岁月 提交于 2019-12-28 04:54:14
问题 Both unique_ptr and shared_ptr accept a custom destructor to call on the object they own. But in the case of unique_ptr , the destructor is passed as a template parameter of the class , whereas the type of shared_ptr 's custom destructor is to be specified as a template parameter of the constructor . template <class T, class D = default_delete<T>> class unique_ptr { unique_ptr(T*, D&); //simplified ... }; and template<class T> class shared_ptr { template<typename D> shared_ptr(T*, D); /

Design of (shared_ptr + weak_ptr) compatible with raw pointers

不羁的心 提交于 2019-12-25 08:39:45
问题 Preamble In C++11 there is std::shared_ptr + std::weak_ptr combo. Despite being very useful, it has a nasty issue: you cannot easily construct shared_ptr from a raw pointer. As a result of this flaw, such smart pointers usually become "viral": people start to completely avoid raw pointers and references, and use exclusively shared_ptr and weak_ptr smart pointers all over the code. Because there is no way to pass a raw reference into a function expecting a smart pointer. On the other hand,

Confusion concerning boost::shared_ptr

我们两清 提交于 2019-12-25 04:41:38
问题 My question revolves around whether or not I must expose my use of the boost::shared_ptr from my interface and whether or not I should expose raw pointers or references from my interface. Consider the case of a Person who has an Employeer . Employeer internally maintains all of its employees in a vector< shared_ptr< Person > > . Because of this, do best practices dictate that any interface involving Person should be a shared_ptr wrapped person? For example, are all or only some of these ok:

Segmentation fault on using shared_ptr with vectors

徘徊边缘 提交于 2019-12-25 03:12:06
问题 I am using a shared_ptr for the first time pardon me If I have made a very silly mistake and help me in overcoming this segmentation fault. I wish to have a private vector which can be read from different classes even if the object is destroyed. Hence I read about std::shared_ptr The code is giving a segfault in storeCounterData function Thanks in Advance for your help !!! main.cpp #include <iostream> #include "counter.hpp" #include "getCounter.hpp" const int max_ports = 3; int main() {

SWIG_SHARED_PTR macro with templated class

让人想犯罪 __ 提交于 2019-12-25 03:08:30
问题 I'm using SWIG with boost shared pointers to create python extensions. My current issue is that the SWIG_SHARED_PTR macro seems to work differently with templated classes. I'll give two examples, one without templates (example), and one with templates (example2). First I'll include the code, and at the end show the difference in behavior of the two extensions within python. The basic problem is that without templates the shared pointers appear in python as <example.Derived; proxy of <Swig

Accessing calloc'd data through a shared_ptr

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-24 16:48:57
问题 I'm trying to access the data that I previously allocated with the calloc method through a shared_ptr. For some reason I can't access it (keeps on crashing with EXC_BAD_ACCESS ) on glTexImage2D (last line of my code snippets). My util method to load the data: shared_ptr<ImageData> IOSFileSystem::loadImageFile(string path) const { // Result shared_ptr<ImageData> result = shared_ptr<ImageData>(); ... // Check if file exists if([[NSFileManager defaultManager] fileExistsAtPath:fullPath

boost::shared_ptr and nullptr in default template function argument

纵然是瞬间 提交于 2019-12-24 11:37:35
问题 So, here is my class and its function: template <class T> class cResourceManager { public: bool add(const std::string & key, boost::shared_ptr<T> ptr = nullptr); private: std::map <const std::string, boost::shared_ptr<T> > resources; }; template <class T> bool cResourceManager<T>::add(const std::string & key, boost::shared_ptr<T> ptr) { if (resources.find(key) == resources.end()) { if(ptr != nullptr) //we have the object { resources.insert(std::pair<const std::string, boost::shared_ptr<T>>

shared_ptr and logical pointer ownership use-case in a complex design

时光毁灭记忆、已成空白 提交于 2019-12-24 09:54:06
问题 I have an Object A that contains a shared resource (shared_ptr) r , A is the creator/owner of r upon construction the object "registers" its r with another object B. Object B holds a reference to A 's r in a std::set . Object A is used as a Boost::asio::handler. I need to unregister r from B when A is being destructed, and when A holds unique access to r , as A is r 's creator it is responsible for destroying it. Note: A is copy constructed multiple times when it is used as an boost::asio

Using C++ to write an image to a RabbitMQ queue

杀马特。学长 韩版系。学妹 提交于 2019-12-24 08:55:45
问题 I'm using SimpleAmqpClient which is a C++ library to use with a RabbitMQ broker. I can send and receive string i.e. "hello world". Here is the program that does that. #include <stdlib.h> #include <stdio.h> #include <SimpleAmqpClient/SimpleAmqpClient.h> #include <iostream> #include "SimplePublisher.h" using namespace AmqpClient; using namespace std; int main() { char *szBroker = getenv("AMQP_BROKER"); Channel::ptr_t channel; if (szBroker != NULL) channel = Channel::Create(szBroker); else

How to fix: error: invalid conversion from 'const MyClass*' to 'MyClass*'

落花浮王杯 提交于 2019-12-24 02:57:30
问题 I am getting this compile error: error: invalid conversion from 'const MyClass*' to 'MyClass*' Here is the code: std::tr1::shared_ptr<MyClass> myClassA; const MyClass* myClassB; myClassA = std::tr1::shared_ptr<MyClass>(myClassB); // error here I think I understand the error, just don't know how to fix. I need myClassB to be a const so how to convert/copy classB to a shared_ptr? 回答1: You'll need a shared pointer to a const object: std::tr1::shared_ptr<const MyClass> myClassA; ^^^^^ 回答2: You