smart-pointers

Replacing auto_ptr in VC++ 8

微笑、不失礼 提交于 2019-12-21 04:06:17
问题 std::auto_ptr is broken in VC++ 8 (which is what we use at work). My main gripe with it is that it allows auto_ptr<T> x = new T(); , which of course leads to horrible crashes, while being simple to do by mistake. From an answer to another question here on stackoverflow: Note that the implementation of std::auto_ptr in Visual Studio 2005 is horribly broken. http://connect.microsoft.com/VisualStudio/feedback/ViewFeedback.aspx?FeedbackID=98871 http://connect.microsoft.com/VisualStudio/feedback

pros and cons of smart pointers

孤人 提交于 2019-12-20 18:01:33
问题 I came to know that smart pointer is used for resource management and supports RAII. But what are the corner cases in which smart pointer doesn't seem smart and things to be kept in mind while using it ? 回答1: Smart pointers don't help against loops in graph-like structures. For example, object A holds a smart pointer to object B and object B - back to object A. If you release all pointers to both A and B before disconnection A from B (or B from A) both A and B will hold each other and form a

QList of QScopedPointers

老子叫甜甜 提交于 2019-12-20 02:42:12
问题 I'm trying to store QScopedPointers in a QList. I found this comment One can also use QList >. – Kuba Ober Jan 14 '14 at 18:17 (first comment on this answer: https://stackoverflow.com/a/21120575/3095014) and this post https://forum.qt.io/topic/59338/solved-qlist-of-qscopedpointers which implies that this should work. But if I try to compile the code of the second link, I'm getting this errors: E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(404) : error C2248: 'QScopedPointer<Label

QList of QScopedPointers

▼魔方 西西 提交于 2019-12-20 02:42:06
问题 I'm trying to store QScopedPointers in a QList. I found this comment One can also use QList >. – Kuba Ober Jan 14 '14 at 18:17 (first comment on this answer: https://stackoverflow.com/a/21120575/3095014) and this post https://forum.qt.io/topic/59338/solved-qlist-of-qscopedpointers which implies that this should work. But if I try to compile the code of the second link, I'm getting this errors: E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(404) : error C2248: 'QScopedPointer<Label

Deep copy constructor with std::vector of smart pointers

吃可爱长大的小学妹 提交于 2019-12-19 04:10:17
问题 Let's say I have a class FooContainer that aggregates unique_ptr objects of type Foo #include <vector> #include <memory> class FooContainer { protected: std::vector<std::unique_ptr<Foo>> many; //other attributes public: FooCoontainer(const FooContainer&); //handling functions for Foo }; The question is how to correctly implement deep copy constructor, and what is syntax for it. Simply assigning FooContainer::FooContainer(const FooContainer& fc) { many=fc.many; } will attempt to copy the

Deep copy constructor with std::vector of smart pointers

无人久伴 提交于 2019-12-19 04:10:04
问题 Let's say I have a class FooContainer that aggregates unique_ptr objects of type Foo #include <vector> #include <memory> class FooContainer { protected: std::vector<std::unique_ptr<Foo>> many; //other attributes public: FooCoontainer(const FooContainer&); //handling functions for Foo }; The question is how to correctly implement deep copy constructor, and what is syntax for it. Simply assigning FooContainer::FooContainer(const FooContainer& fc) { many=fc.many; } will attempt to copy the

How does a weak_ptr know that the shared resources has expired?

流过昼夜 提交于 2019-12-18 19:28:34
问题 Considering the following code: #include <memory> #include <iostream> using namespace std; struct MySharedStruct { int i; }; void print_value_of_i(weak_ptr<MySharedStruct> weakPtr) { if (shared_ptr<MySharedStruct> sp = weakPtr.lock()) { cout << "Value of i = " << sp->i << endl; } else { cout << "Resource has expired"; } } int main() { shared_ptr<MySharedStruct> sharedPtr(new MySharedStruct() ); sharedPtr->i = 5; weak_ptr<MySharedStruct> weakPtr; weakPtr = sharedPtr; print_value_of_i(weakPtr);

How to approach copying objects with smart pointers as class attributes?

强颜欢笑 提交于 2019-12-18 16:47:11
问题 From the boost library documentation I read this: Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed. I have a very simple problem: I want to use RAII for pointer attributes of a class that is Copyable and Assignable. The copy and assignment operations should be deep: every object should have its own copy of the actual data. Also, RTTI needs to be available for the attributes (their type may also

How to approach copying objects with smart pointers as class attributes?

老子叫甜甜 提交于 2019-12-18 16:47:10
问题 From the boost library documentation I read this: Conceptually, smart pointers are seen as owning the object pointed to, and thus responsible for deletion of the object when it is no longer needed. I have a very simple problem: I want to use RAII for pointer attributes of a class that is Copyable and Assignable. The copy and assignment operations should be deep: every object should have its own copy of the actual data. Also, RTTI needs to be available for the attributes (their type may also

C++11 smart pointers and polymorphism

风格不统一 提交于 2019-12-18 12:05:56
问题 I'm rewriting an application using c++11 smart pointers. I have a base class: class A {}; And a derived class: class B : public A { public: int b; }; I have another class containing a vector with either A or B objects: class C { public: vector<shared_ptr<A>> v; }; I have no problem constructing C with A (base class) objects but how can I fill it with B (derived class) objects? I'm trying this: for(int i = 0; i < 10; i++) { v.push_back(make_shared<B>()); v.back()->b = 1; }; And the compiler