smart-pointers

Custom deleters for std::shared_ptrs

为君一笑 提交于 2019-12-12 10:39:45
问题 Is it possible to use a custom deleter after creating a std::shared_ptr without using new ? My problem is that object creation is handled by a factory class and its constructors & destructors are protected, which gives a compile error, and I don't want to use new because of its drawbacks. To elaborate: I prefer to create shared pointers like this, which doesn't let you set a custom deleter (I think): auto sp1 = make_shared<Song>(L"The Beatles", L"Im Happy Just to Dance With You"); Or I can

How to return a private pointer to a list of pointers as const?

孤者浪人 提交于 2019-12-12 10:05:04
问题 I have a pointer to a list of pointers, as a private variable. I also have a getter that returns the pointer to the list. I need to protect it from changes. I couldn't find how to use reinterpret_cast or const_cast on this. class typeA{ shared_ptr<list<shared_ptr<typeB>>> l; public: shared_ptr<list<shared_ptr<const typeB>>> getList(){return (l);}; }; The compiler returns: error: could not convert ‘((typeA*)this)->typeA::x’ from ‘std::shared_ptr<std::__cxx11::list<std::shared_ptr<typeB> > >’

delete via a pointer to Derived, not Base

房东的猫 提交于 2019-12-12 06:57:37
问题 I implemented a basic Smart pointer class. It works for the following type of code. (considering Base1 has a public constructor) Sptr<Base1> b(new Base1); b->myFunc(); { Sptr<Base1> c = b; Sptr<Base1> d(b); Sptr<Base1> e; e = b; } But in the test code it has a protected constructor(I need it to be this way). and the code Sptr<Base1> sp(new Derived); Produces the following error (notice the Derived): Sptr.cpp: In instantiation of ‘my::Sptr<T>::~Sptr() [with T = Base1]’: Sptr.cpp:254:39:

C++ Assigning this pointer of a class to either a unique_ptr or a shared_ptr

﹥>﹥吖頭↗ 提交于 2019-12-12 03:56:45
问题 I have a base class that I want to inherit from and before any of its derived classes can be declared at least 1 instance of the base class must be declared first. I was thinking about storing the this pointer of the base class into its own member variable of a unique_ptr instead of using a static_ptr . Also the base class will keep track of all instances of its derived classes until another base class is declared. Here is what my class declarations look like: #include <vector> #include <map>

QCache and std::shared_ptr

时光怂恿深爱的人放手 提交于 2019-12-12 03:22:40
问题 Can someone give me please a hint for this error: no viable conversion from 'std::shared_ptr<Foo>' to 'std::__1::shared_ptr<Foo> *' The QCache looks like this: QCache<int, std::shared_ptr<Foo>> cache; And I try to insert the element like this: std::shared_ptr<Foo> foo; cache.insert(23, foo); Thanks for your help. 回答1: Just looked into QCache API, and since my guess is correct, I will post it as an answer (with hopes for upvotes!). Signature for insert() is bool QCache::insert(const Key & key,

returning reference from method

三世轮回 提交于 2019-12-12 03:11:03
问题 class A : boost::noncopyable{ }; class B{ A & conn() const; }; How would i declare and implement conn() given that: conn should create and return a reference to an object of type A. i can't break B's interface in client code using B. i want to prevent my code to leak memory, so i cannot simply return references to objects in heap. I didn't find any implementation of smart pointers that wouldn't break client code since there's no conversion to type A*, and i left client code untouched then i'd

What is the size of an auto_ptr?

Deadly 提交于 2019-12-12 03:09:39
问题 Does an auto_ptr have the same size as a pointer? I have to substitute it with a boost::scoped_ptr , and I was wondering if these two data types have the same size. 回答1: You can pretty easily find out what the sizes are by simply doing this: #include <iostream> #include <memory> #include <boost/shared_ptr.hpp> #include <boost/scoped_ptr.hpp> int main() { std::cout << "raw pointer: " << sizeof(int*) << std::endl; std::cout << "auto-ptr: " << sizeof(std::auto_ptr<int>) << std::endl; std::cout <

Compiler error while using shared_ptr with a pointer to a pointer

情到浓时终转凉″ 提交于 2019-12-11 18:54:08
问题 I am new to using smart pointers in C++ and my current issue is that I am converting C code to C++ (C++11/14/17) and I am having some problems understanding using shared_ptr with a pointer to pointer. I have derived a toy example which I believe illustrates the problem Following is the header file #include <memory> using std::shared_ptr; struct DataNode { shared_ptr<DataNode> next; } ; struct ProxyNode { shared_ptr<DataNode> pointers[5]; } ; struct _test_ { ProxyNode** flane_pointers; }; And

How can I enforce single ownership with weak_ptr? (Or, how to subclass shared_ptr and override dtor)

落爺英雄遲暮 提交于 2019-12-11 18:36:32
问题 So, I've done (a small amount) of reading and am aware that unique_ptr in combination with raw pointers is the pattern to use when modeling unique ownership. However, I really like the simple and clear concept of using a weak_ptr to check if a value is valid, and then discard the shared_ptr after using it, and this keeps everyone happy (at a slight reference counting performance cost). My particular problem right now is in creating an expressive and flexible system for tracking multitouch

Workaround for Making a unique_ptr to a Forward Declared Type

牧云@^-^@ 提交于 2019-12-11 17:58:54
问题 Given that I have a forward declared type: class Foo; I want to make a unique_ptr to this type: unique_ptr<Foo> pFoo; This works fine in visual-studio-2017 but I can't make it work in visual-studio-2012. C:\Program Files (x86)\Microsoft Visual Studio 11.0\VC\include\memory(1150): error C2027: use of undefined type Foo (....\src\STETestbed\STETestbed.cpp) O:\Engine\stetestbed\include\STETestbed\ComponentDirector.h(26) : see declaration of Foo C:\Program Files (x86)\Microsoft Visual Studio 11.0