smart-pointers

C++11 make_shared instancing

时光怂恿深爱的人放手 提交于 2019-12-05 05:48:59
Apologies for the long question, but some context is necessary. I have a bit of code that seems to be a useful pattern for the project I'm working on: class Foo { public: Foo( int bar = 1 ); ~Foo(); typedef std::shared_ptr< Foo > pointer_type; static pointer_type make( int bar = 1 ) { return std::make_shared< Foo >( bar ); } ... } As you can see, it provides a straightforward way of constructing any class as a PointerType which encapsulates a shared_ptr to that type: auto oneFoo = Foo::make( 2 ); And therefore you get the advantages of shared_ptr without putting references to make_shared and

Wrap C allocation for RAII

…衆ロ難τιáo~ 提交于 2019-12-05 05:16:43
I've these plain C functions from a library: struct SAlloc; SAlloc *new_salloc(); void free_salloc(SAlloc *s); Is there any way I can wrap this in C++ to a smart pointer (std::unique_ptr), or otherwise a RAII wrapper ? I'm mainly curious about the possibilities of the standard library without creating my own wrapper/class. Yes, you can reuse unique_ptr for this. Just make a custom deleter. struct salloc_deleter { void operator()(SAlloc* s) const { free_salloc(s); // what the heck is the return value for? } } using salloc_ptr = std::unique_ptr<SAlloc, salloc_deleter>; I like R. Martinho

Static method to create an object instead of constructor

本秂侑毒 提交于 2019-12-05 04:17:01
I'm creating a GUI in my C++ application and I have a class called GUIObject which is base class for all other components, like for example Button , CheckBox , Window etc. I also have a class GUIObjectsStorage , which consists of all GUIObject s that are created. So far I've been working with raw pointers, so I just had this constructor for GUIObject class: GUIObject::GUIObject() : { GUIObjectsStorage::Instance().addObject(this); } And it was ok for my needs, because whenever I wanted to access specific object, I just took it from GUIObjectsStorage . But now I'm trying to move into use of

Undefined reference error when initializing unique_ptr with a static const

主宰稳场 提交于 2019-12-05 04:06:00
When I try to use a static const to initialize a unique_ptr , I get an "undefined reference" error. However, when I new a pointer using the same constant, the symbol seems to be magically defined. Here is a simple program that reproduces the error: Outside_library.h class Outside_library { public: static const int my_const = 100; }; main.cpp #include "Outside_library.h" #include <iostream> #include <memory> class My_class { public: My_class(int num) { m_num = num; }; virtual ~My_class(){}; private: int m_num; }; int main(int, char* []) { My_class* p_class = new My_class(Outside_library::my

intrusive_ptr: Why isn't a common base class provided?

天涯浪子 提交于 2019-12-05 03:01:00
boost::intrusive_ptr requires intrusive_ptr_add_ref and intrusive_ptr_release to be defined. Why isn't a base class provided which will do this? There is an example here: http://lists.boost.org/Archives/boost/2004/06/66957.php , but the poster says "I don't necessarily think this is a good idea". Why not? Update: I don't think the fact that this class could be misused with Multiple Inheritance is reason enough. Any class which derives from multiple base classes with their own reference count would have the same issue. Whether these refcounts are implemented via a base class or not makes no

Boost shared_ptr: How to use custom deleters and allocators

|▌冷眼眸甩不掉的悲伤 提交于 2019-12-05 02:49:13
问题 Free function allocate_shared can be used with any standard compliant allocator. But what about shared_ptr's constructor and reset method. template<class Y, class D, class A> shared_ptr(Y * p, D d, A a); template<class Y, class D, class A> void reset(Y * p, D d, A a); The manual says that D should provide a call operator which will be used to delete the pointer and A must be a standard compliant allocator. If so, why D is needed? Can't A do both allocation and delocation? Don't you think that

How to enforce users to create objects of class derived from mine with “new” only?

左心房为你撑大大i 提交于 2019-12-05 02:10:28
问题 To implement reference counting we use an IUnknown-like interface and a smart pointer template class. The interface has implementation for all the reference-count methods, including Release() : void IUnknownLike::Release() { if( --refCount == 0 ) { delete this; } } The smart pointer template class has a copy constructor and an assignment operator both accepting raw pointers. So users can do the following: class Class : public IUnknownLike { }; void someFunction( CSmartPointer<Class> object );

auto_ptr or shared_ptr equivalent in managed C++/CLI classes

孤街醉人 提交于 2019-12-05 02:06:17
In C++/CLI , you can use native types in a managed class by it is not allowed to hold a member of a native class in a managed class : you need to use pointers in that case. Here is an example : class NativeClass { .... }; public ref class ManagedClass { private: NativeClass mNativeClass; // Not allowed ! NativeClass * mNativeClass; // OK auto_ptr<NativeClass> mNativeClass; //Not allowed ! boost::shared_ptr<NativeClass> mNativeClass; //Not allowed ! }; Does anyone know of an equivalent of shared_ptr in the C++/CLI world? Edit: Thanks for your suggestion, "1800-Information". Following your

Is this a valid use of intrusive_ptr?

北城以北 提交于 2019-12-05 01:11:42
问题 Within my code I follow two rules when it comes to intrusive_ptrs: Passing a raw pointer by value implies that the raw pointer is guaranteed to be valid during the lifetime of that function. If the raw pointer is to be stored and used beyond the lifetime of the function, it should be stored in an intrusive_ptr. Many Internet commenters have written that shared_ptr should be preferred over intrusive_ptr except when working with third party code. However, intrusive_ptr obviates passing around

Array of polymorphic objects

旧城冷巷雨未停 提交于 2019-12-05 00:41:04
I commonly come across the need to create arrays or vectors of polymorphic objects. I'd usually prefer to use references, rather than smart pointers, to the base class because they tend to be simpler. Arrays and vectors are forbidden from containing raw references, and so I've tended to use smart pointers to the base classes instead. However, there is also the option to use std::reference_wrapper instead: https://en.cppreference.com/w/cpp/utility/functional/reference_wrapper From what I can tell from the documentation, this is what one of its intended uses is, but when the topic of arrays