shared-ptr

shared_ptr in std::tr1

主宰稳场 提交于 2019-12-21 09:03:47
问题 I am working on a platform with a gcc compiler however boost cannot compile on it. I am wondering what is the proper way to include the shared_ptr in std:tr1 on gcc? the file i looked in said not to include it directly, from what i can tell no other file includes it either :| 回答1: In G++ 4.3 , #include <tr1/memory> should do the trick. You'll find shared_ptr at std::tr1::shared_ptr . 回答2: Boost itself has the answer. 回答3: Boost can not compile on it? Most of the boost library doesn't need to

Does it make sense to check for nullptr in custom deleter of shared_ptr?

倾然丶 夕夏残阳落幕 提交于 2019-12-21 07:15:27
问题 I've seen some code that uses std::shared_ptr with a custom deleter that test the argument for nullptr, for example, MyClass which has a close() method and is constructed with some CreateMyClass : auto pMyClass = std::shared_ptr<MyClass>(CreateMyClass(), [](MyClass* ptr) { if(ptr) ptr->close(); }); Does it make sense to test ptr for null-ness in the deleter? Can this happen? how? 回答1: The constructor std::shared_ptr<T>::shared_ptr(Y*p) has the requirement that delete p is a valid operation.

Strategy pattern in C++. Implementation options

早过忘川 提交于 2019-12-21 05:41:11
问题 Here's a simplified example of what is called (I hope - please, correct me if I'm wrong) Strategy pattern: there's a class FileWriter which writes key-value pairs to a file and uses object of IFormatter interface for formatting text being written. There are different formatters implementations and formatter object is passed when FileWriter is created. Here's one (bad) implementation of such pattern: #include <iostream> #include <fstream> #include <stdlib.h> #include <sstream> using namespace

Strategy pattern in C++. Implementation options

不羁岁月 提交于 2019-12-21 05:41:06
问题 Here's a simplified example of what is called (I hope - please, correct me if I'm wrong) Strategy pattern: there's a class FileWriter which writes key-value pairs to a file and uses object of IFormatter interface for formatting text being written. There are different formatters implementations and formatter object is passed when FileWriter is created. Here's one (bad) implementation of such pattern: #include <iostream> #include <fstream> #include <stdlib.h> #include <sstream> using namespace

std::shared_ptr: reset() vs. assignment

ぃ、小莉子 提交于 2019-12-20 10:59:44
问题 This is a basic question, but I did not find a previous post about it. The title of the following question sounds like it might be the same question as mine, but the question itself does not match the title: is it better to use shared_ptr.reset or operator =? I am confused about the purpose of the reset() member function of std::shared_ptr : what does it contribute in addition to the assignment operator? To be concrete, given the definition: auto p = std::make_shared<int>(1); Are the

What's the best strategy for typedef'ing shared pointers?

谁都会走 提交于 2019-12-20 09:53:18
问题 I have a quick question regarding the use of typedefs for lengthy templates. The crux: I've found myself in something of a pickle—there doesn't seem to be a good place to place typedefs except local to client functions. While there are similar SO questions (see here for example), none seem to address this exactly. Please note that this question doesn't address whether typedefs are desirable in what follows—I've tried to simplify things for expository purposes. My problem has arisen while

Custom (pool) allocator with boost shared_ptr

大憨熊 提交于 2019-12-20 08:49:21
问题 I want objects managed by a shared_ptr to be allocated from a pool, say Boost's Pool interface, how can this be achieved? 回答1: 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

Ensuring shared pointers within vector are pushed back properly [closed]

余生颓废 提交于 2019-12-20 07:45:10
问题 Closed. This question is off-topic. It is not currently accepting answers. Want to improve this question? Update the question so it's on-topic for Stack Overflow. Closed 2 years ago . I might have some aspects of this wrong, this is really the first time I have dealt much with shared pointers in particular. I am working on traversing a tree. My tree consists of a linked list, with a vector of shared pointers representing all children for each node. To traverse, I am (to begin with) trying to

Assigning shared_ptr to weak_ptr

杀马特。学长 韩版系。学妹 提交于 2019-12-20 06:14:05
问题 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. 回答1: 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

find out the class type inside the class and its children

拥有回忆 提交于 2019-12-20 06:00:42
问题 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