shared-ptr

Custom (pool) allocator with boost shared_ptr

杀马特。学长 韩版系。学妹 提交于 2019-12-02 17:09:22
I want objects managed by a shared_ptr to be allocated from a pool, say Boost's Pool interface, how can this be achieved? Here's the code to do what you want (probably won't compile as I don't have boost on hand and I'm writing it from memory): class YourClass; // your data type, defined somewhere else boost::object_pool<YourClass> allocator; void destroy(YourClass* pointer) { allocator.destroy(pointer); } boost::shared_ptr<YourClass> create() { // usage of object_pool<??>::construct requires that you have a // YourClass::YourClass(void) defined. If you need to pass arguments // to the new

Using smart pointers for class members

只愿长相守 提交于 2019-12-02 13:46:49
I'm having trouble understanding the usage of smart pointers as class members in C++11. I have read a lot about smart pointers and I think I do understand how unique_ptr and shared_ptr / weak_ptr work in general. What I don't understand is the real usage. It seems like everybody recommends using unique_ptr as the way to go almost all the time. But how would I implement something like this: class Device { }; class Settings { Device *device; public: Settings(Device *device) { this->device = device; } Device *getDevice() { return device; } }; int main() { Device *device = new Device(); Settings

C++ 11: smart pointers usage [duplicate]

亡梦爱人 提交于 2019-12-02 13:20:27
This question already has an answer here: Which kind of pointer do I use when? 4 answers What are the best practices for using smart. Are there situations in which i should prefer using raw pointer instead of smart pointers? For example, if i know that class A creates class B and is the only owner of B - if there a reason to use smart pointers? If you know of any good articles on that subject, please share. If the pointer owns the object at any time, then use a smart pointer. If the pointer does not own the object (i.e. the object is owned by another smart pointer and guaranteed to outlive

Assigning shared_ptr to weak_ptr

℡╲_俬逩灬. 提交于 2019-12-02 12:49:45
I want to assign constructed shared_ptr to weak_ptr: std::weak_ptr<void> rw = std::shared_ptr<void>(operator new(60), [](void *pi) { operator delete(pi); }); But, when I do rw.expired() , it shows expired means it is empty. Any suggestions where I am going wrong? Thanks in advance. Slava Purpose of std::shared_ptr is to release managed object when last shared pointer which points to it is destroyed or reassigned to somewhere else. You created a temporary shared ptr, assgned it to std::weak_ptr and then it is just destroyed at the end of the expression. You need to keep it alive: auto shared =

find out the class type inside the class and its children

◇◆丶佛笑我妖孽 提交于 2019-12-02 10:58:41
Lets say I have the class template<typename PointT> class Parent { public: typedef boost::shared_ptr<Parent<PointT> > Ptr; inline Ptr makeShared () { return Ptr (new Parent<PointT> (*this)); } }; template<typename PointT> class Child : public Parent { public: typedef boost::shared_ptr<Child<PointT> > Ptr; }; Now what I'd like to rewrite the definition of Ptr and makeShared() to be generic, so that calling makeShared() from child class(es) instances would yield a pointer to the child class not the parent To make it more clear calling makeShared() on any class inheriting Parent would give a

Ownership with a physical representation

为君一笑 提交于 2019-12-02 09:19:41
问题 After reading on RAII, viewing Herb Sutter's CppCon2014 presentation, and reading the core guidelines and related articles over the course of some days, I'm still quite confused on ownership and related semantics. Let's say class A and class B represent physical entities, and there's a Scene class and a Process class. The Process class is a main function, if you will. In the real world, an A can acquire a B and physically keep it for itself. It can also release it. During the course of a

Ownership with a physical representation

一个人想着一个人 提交于 2019-12-02 06:46:35
After reading on RAII, viewing Herb Sutter's CppCon2014 presentation , and reading the core guidelines and related articles over the course of some days, I'm still quite confused on ownership and related semantics. Let's say class A and class B represent physical entities, and there's a Scene class and a Process class. The Process class is a main function, if you will. In the real world, an A can acquire a B and physically keep it for itself. It can also release it. During the course of a Process instance, an A object must therefore be able to have for itself a B instance, and also to release

Implementing a simple singly linked list with smart pointers

天大地大妈咪最大 提交于 2019-12-02 03:07:54
问题 Hi I'm trying to implement a simple singly linked list using smart pointers, here is what I have so far, I opted with using C++'s shared_ptr but I read that a unique_ptr would be more appropriate for this case but, I don't really know how you would iterate over the list (i.e currentNode = currentNode->next) to get to the end of the list in order to insert an element using a unique_ptr. Here is the code I have so far: template <typename T> class LinkedList; template <typename T> class ListNode

implicit instantiation of undefined template: Boost Bug or Clang Bug?

泄露秘密 提交于 2019-12-02 01:54:38
I was trying to compile some code that uses Boost (1.49), with Clang(& libc++) from trunk. The problematic code boils down to the following: #include <memory> #include <boost/signals2.hpp> int main() { std::shared_ptr<int> s; } When compiled with Clang, the following message is emmited: $ clang++ -I/home/alexander/usr/local/include --stdlib=libc++ -std=c++0x signals2-bug.cpp -o signals2-bug signals2-bug.cpp:6:26: error: implicit instantiation of undefined template 'std::shared_ptr<int>' std::shared_ptr<int> s; ^ /home/alexander/usr/local/include/boost/signals2/detail/foreign_ptr.hpp:24:30:

boost::shared_ptr question. Why does this work?

十年热恋 提交于 2019-12-01 23:50:11
问题 In experimenting with this question I created an example that I utterly do not understand. In particular, it highlights my misunderstanding of pointers, references, and the boost::shared_ptr. int& r = *(new int(0));//gratuitous pointer leak, got to initialize it to something { boost::shared_ptr<int> sp(new int(100)); r = *sp; cout << "r=" << r << endl; } cout << "r=" << r << endl << endl; int* p; { boost::shared_ptr<int> sp(new int(100)); p = &*sp; cout << "*p=" << *p << endl; } cout << "*p="