smart-pointers

enable_shared_from_this and inheritance

陌路散爱 提交于 2019-12-04 15:15:51
问题 I've got a type which inherits from enable_shared_from_this<type> , and another type that inherits from this type. Now I can't use the shared_from_this method because it returns the base type and in a specific derived class method I need the derived type. Is it valid to just construct a shared_ptr from this directly? Edit: In a related question, how can I move from an rvalue of type shared_ptr<base> to a type of shared_ptr<derived> ? I used dynamic_cast to verify that it really was the

Smart pointers with a library written in C

喜夏-厌秋 提交于 2019-12-04 14:22:47
问题 I'm using C++ with the OpenCV library, which is a library image-processing although that's not relevant for this question. Currently I have a design decision to make. OpenCV, being a C library, has its data structures (such as CvMat) declared as structs. To create them, you use functions like cvCreateMat, and to release them, you use functions like cvReleaseMat. Being a C++ programmer, I created a special cv_scoped class which would automatically call cvReleaseMat when it went out of scope

Implementing Containers using Smart Pointers

流过昼夜 提交于 2019-12-04 11:16:06
Ok, so everyone knows that raw pointers should be avoided like the plague and to prefer smart pointers, but does this advice apply when implementing a container? This is what I am trying to accomplish: template<typename T> class AVLTreeNode { public: T data; unique_ptr<AVLTreeNode<T>> left, right; int height; } Unique_ptr can make container functions more cumbersome to write because I can't have multiple raw pointers temporarily pointing to the same object in a way that is elegant. For example: unique_ptr<AVLTreeNode<T>> rotate_right(unique_ptr<AVLTreeNode<T>> n1) { unique_ptr<AVLTreeNode<T>>

Benefits of using BOOST shared_array over shared_ptr

生来就可爱ヽ(ⅴ<●) 提交于 2019-12-04 09:57:02
问题 I want to use BOOST Smart pointer for memory management in my application. But I'm not sure which smart pointer should I use for dynamically allocated array shared_ptr or shared_array . According to the BOOST doc Starting with Boost release 1.53, shared_ptr can be used to hold a pointer to a dynamically allocated array. So I'm just wondering now what purpose user should use shared_array instead of shared_ptr . 回答1: Before boost 1.53, shared_ptr is to be used for a pointer to a single object.

Mixing C++ and Objective-C

元气小坏坏 提交于 2019-12-04 09:33:27
问题 I am using C++ as the app backbone and Objective-C for the GUI, that's fine. But when it comes to mixing those code together in Objective-C++ (.mm file), I have got a few question: 1. Can I mix STL containers with Objective-C or Cocos2D objects? E.g. In Objective-C header, can I do the following? #include <vector> #include <boost\shared_ptr.hpp> @interface MyClass : NSObject { std::vector<boost::shared_ptr<CCSprite> > m_spriteList; } And then in the .mm file, I want to do CCSprite* newSprite

How to force only smart pointers instance for a class?

醉酒当歌 提交于 2019-12-04 09:16:14
问题 I've been working on a way to prevent user of using a class without smart pointers. Thus, forcing them to have the object being heap allocated and managed by smart pointers. In order to get such a result, I've tried the following : #include <memory> class A { private : ~A {} // To force use of A only with std::unique_ptr friend std::default_delete<A>; }; This work pretty well if you only want your class users being capable of manipulating instance of your class through std::unique_ptr . But

Vector of shared pointers , memory problems after clearing the vector

不羁的心 提交于 2019-12-04 07:58:35
问题 I realized that after calling vector.clear() which hold shared pointers, the destructors of the object which own by shared_ptr is not being released. Code example can be seen below . Even vector.clear() being called, destructor called after shared pointer goes beyond the scope.My question is - do I have to delete all smart pointers inside the vector manually by resetting them? Is there any easier way that you can advice ? Output : constructor I am here destructor Code: #include <vector>

C++: Creating a shared object rather than a shared pointer to an object

半腔热情 提交于 2019-12-04 03:44:01
问题 boost::shared_ptr really bothers me. Certainly, I understand the utility of such a thing, but I wish that I could use the shared_ptr<A> as an A* . Consider the following code class A { public: A() {} A(int x) {mX = x;} virtual void setX(int x) {mX = x;} virtual int getX() const {return mX;} private: int mX; }; class HelpfulContainer { public: //Don't worry, I'll manager the memory from here. void eventHorizon(A*& a) { cout << "It's too late to save it now!" << endl; delete a; a = NULL; } };

Boost.Pointer Container made obsolete by std::unique_ptr in C++11/14?

时光总嘲笑我的痴心妄想 提交于 2019-12-04 02:50:47
Does std::unique_ptr make Boost.Pointer Container library obsolete in C++11/14? In C++98/03 there isn't move semantics, and a smart pointer like shared_ptr has reference-counting related overhead (both for the ref counting block, and the interlocked increment/decrements) if compared to raw pointers. So something like std::vector<shared_ptr<T>> has overhead if compared to std::vector<T*> . But is std::vector<std::unqiue_ptr<T>> just as efficient as std::vector<T*> (no reference counting overhead), and in addition safe in regard to exceptions and automatic destruction (i.e. vector<unique_ptr<T>>

Atomic operations on `unique_ptr`

烂漫一生 提交于 2019-12-04 00:18:55
问题 std::shared_ptr has specializations for atomic operations like atomic_compare_exchange_weak and family, but I cannot find documentation on equivalent specializations for std::unique_ptr . Are there any? If not, why not? 回答1: No there no standard atomic functions for std::unique_ptr . I did find an argument for why not in Atomic Smart Pointers(N4058) by Herb Sutter Lawrence Crowl responded to add: One of the reasons that shared_ptr locking is the way it is is to avoid a situation in which we