smart-pointers

C++ 11: smart pointers usage [duplicate]

亡梦爱人 提交于 2019-12-02 13:20:27
This question already has an answer here: Which kind of pointer do I use when? 4 answers What are the best practices for using smart. Are there situations in which i should prefer using raw pointer instead of smart pointers? For example, if i know that class A creates class B and is the only owner of B - if there a reason to use smart pointers? If you know of any good articles on that subject, please share. If the pointer owns the object at any time, then use a smart pointer. If the pointer does not own the object (i.e. the object is owned by another smart pointer and guaranteed to outlive

What is the difference between Rc<RefCell<T>> and RefCell<Rc<T>>?

谁都会走 提交于 2019-12-02 09:21:37
问题 The Rust documentation covers Rc<RefCell<T>> pretty extensively but doesn't go into RefCell<Rc<T>> , which I am now encountering. Do these effectively give the same result? Is there an important difference between them? 回答1: Do these effectively give the same result? They are very different. Rc is a pointer with shared ownership while RefCell provides interior mutability. The order in which they are composed makes a big difference to how they can be used. Usually, you compose them as Rc

Ownership with a physical representation

为君一笑 提交于 2019-12-02 09:19:41
问题 After reading on RAII, viewing Herb Sutter's CppCon2014 presentation, and reading the core guidelines and related articles over the course of some days, I'm still quite confused on ownership and related semantics. Let's say class A and class B represent physical entities, and there's a Scene class and a Process class. The Process class is a main function, if you will. In the real world, an A can acquire a B and physically keep it for itself. It can also release it. During the course of a

What is the difference between Rc<RefCell<T>> and RefCell<Rc<T>>?

删除回忆录丶 提交于 2019-12-02 07:41:58
The Rust documentation covers Rc<RefCell<T>> pretty extensively but doesn't go into RefCell<Rc<T>> , which I am now encountering. Do these effectively give the same result? Is there an important difference between them? Do these effectively give the same result? They are very different. Rc is a pointer with shared ownership while RefCell provides interior mutability. The order in which they are composed makes a big difference to how they can be used. Usually, you compose them as Rc<RefCell<T>> ; the whole thing is shared and each shared owner gets to mutate the contents. The effect of mutating

Ownership with a physical representation

一个人想着一个人 提交于 2019-12-02 06:46:35
After reading on RAII, viewing Herb Sutter's CppCon2014 presentation , and reading the core guidelines and related articles over the course of some days, I'm still quite confused on ownership and related semantics. Let's say class A and class B represent physical entities, and there's a Scene class and a Process class. The Process class is a main function, if you will. In the real world, an A can acquire a B and physically keep it for itself. It can also release it. During the course of a Process instance, an A object must therefore be able to have for itself a B instance, and also to release

why std::unique_ptr vector gets invalid pointer exception

坚强是说给别人听的谎言 提交于 2019-12-02 04:14:22
I wrote simple code to help me understand smart pointers: string s = "str"; vector <unique_ptr<string>> pv ; pv.push_back(unique_ptr<string>(&s)); cout<<*(pv[0])<<endl; This code compiles fine, but gets me a runtime error: str * Error in `...': munmap_chunk(): invalid pointer: 0x00007ffd956e57e0 * Aborted (core dumped) What happened and what have I done wrong? In the std::unique_ptr 's destructor it will call delete on the &s pointer which was not allocated via new . Just use: std::vector<std::string> vector; vector.emplace_back("str"); std::cout << pv[0] << std::endl; There's no need for std:

auto_ptr with swig

左心房为你撑大大i 提交于 2019-12-02 03:30:37
问题 I'm trying to wrap a C++ library which uses auto_ptr. I'm using swig and want to generate python bindings. I'v seen the section of the swig docu on how to use swig with smart pointers here. But I can't get it to work. Swig generates code that wants to initialize the auto_ptr using a const reference, but auto_ptr defines the copy constructor with a non-const reference e.g. auto_ptr(auto_ptr &). The generated code does not compile with "discards const qualifiers". When I manually delete the

Implementing a simple singly linked list with smart pointers

天大地大妈咪最大 提交于 2019-12-02 03:07:54
问题 Hi I'm trying to implement a simple singly linked list using smart pointers, here is what I have so far, I opted with using C++'s shared_ptr but I read that a unique_ptr would be more appropriate for this case but, I don't really know how you would iterate over the list (i.e currentNode = currentNode->next) to get to the end of the list in order to insert an element using a unique_ptr. Here is the code I have so far: template <typename T> class LinkedList; template <typename T> class ListNode

Why an incomplete type is detected in clang inside a template method?

南笙酒味 提交于 2019-12-02 01:24:44
问题 Today, I encountered a compile issue in clang that surprised me. I guess is reasonable but I like to dig deeper and hear more details. Some standard references if possible also. I have a class with a template method which rely on a member which his type is undefined in the header (but not in the source). Something like the following: // Menu.h class Page; class Menu { public: .... // stuff template<class Visitor> void VisitWidget( Visitor&& visitor); private: std::unique_ptr<Page> m_page; //

auto_ptr with swig

╄→尐↘猪︶ㄣ 提交于 2019-12-02 00:57:39
I'm trying to wrap a C++ library which uses auto_ptr. I'm using swig and want to generate python bindings. I'v seen the section of the swig docu on how to use swig with smart pointers here . But I can't get it to work. Swig generates code that wants to initialize the auto_ptr using a const reference, but auto_ptr defines the copy constructor with a non-const reference e.g. auto_ptr(auto_ptr &). The generated code does not compile with "discards const qualifiers". When I manually delete the const qualifier the code compiles fine. I'v seen lots of mailing list entries but nothing helped. Can