smart-pointers

In the case of using a std::unique_ptr to automatically deallocate memory upon exiting a scoped block, why not just use the stack?

你。 提交于 2020-03-04 18:44:25
问题 This is a great answer about smart pointers, such as unique pointers: What is a smart pointer and when should I use one?. Here is an example they provide as the simplest use of a unique pointer: void f() { { std::unique_ptr<MyObject> ptr(new MyObject(my_constructor_param)); ptr->DoSomethingUseful(); } // ptr goes out of scope -- // the MyObject is automatically destroyed. // ptr->Oops(); // Compile error: "ptr" not defined // since it is no longer in scope. } However, this begs the question:

Unwrap and access T from an Option<Rc<RefCell<T>>>

烂漫一生 提交于 2020-03-03 01:14:37
问题 I am trying to solve some Leetcode problems with Rust. However, I ran into some difficulties with LeetCode's TreeNode implementation. use std::cell::RefCell; use std::rc::Rc; // TreeNode data structure #[derive(Debug, PartialEq, Eq)] pub struct TreeNode { pub val: i32, pub left: Option<Rc<RefCell<TreeNode>>>, pub right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { #[inline] pub fn new(val: i32) -> Self { TreeNode { val, left: None, right: None, } } } If I want to do an inorder traversal,

Unwrap and access T from an Option<Rc<RefCell<T>>>

我只是一个虾纸丫 提交于 2020-03-03 01:09:06
问题 I am trying to solve some Leetcode problems with Rust. However, I ran into some difficulties with LeetCode's TreeNode implementation. use std::cell::RefCell; use std::rc::Rc; // TreeNode data structure #[derive(Debug, PartialEq, Eq)] pub struct TreeNode { pub val: i32, pub left: Option<Rc<RefCell<TreeNode>>>, pub right: Option<Rc<RefCell<TreeNode>>>, } impl TreeNode { #[inline] pub fn new(val: i32) -> Self { TreeNode { val, left: None, right: None, } } } If I want to do an inorder traversal,

Wrapping C create and destroy functions using a smart pointer

♀尐吖头ヾ 提交于 2020-02-26 09:55:12
问题 I have some C API that handles object creation and destruction, it provides: createObject(...) and destroy(...) . I want to wrap it into some more modern construction/destruction mechanisms and use them with smart pointers. I am afraid that at some point I will forget to destroy the object, or some exception will occur. I am aware of custom deleter function for shared_ptr , but I can't explicitly call new , because createOjbect function handles initialization. Can I use STL smart pointers in

C++11 make_shared instancing

北城余情 提交于 2020-02-21 11:43:15
问题 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(

C++11 make_shared instancing

白昼怎懂夜的黑 提交于 2020-02-21 11:40:55
问题 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(

Is there a way to distingush between different `Rc`s of the same value?

試著忘記壹切 提交于 2020-01-24 10:14:45
问题 Here's an example: use std::rc::Rc; #[derive(PartialEq, Eq)] struct MyId; pub fn main() { let rc_a_0 = Rc::new(MyId); let rc_a_1 = rc_a_0.clone(); let rc_b_0 = Rc::new(MyId); let rc_b_1 = rc_b_0.clone(); println!("rc_a_0 == rc_a_1: {:?}", rc_a_0 == rc_a_1); println!("rc_a_0 == rc_b_0: {:?}", rc_a_0 == rc_b_0); } Both println! s above print true . Is there a way distinguish between the rc_a_* and rc_b_* pointers? 回答1: You can cast &*rc to *const T to get a pointer to the underlying data and

Is there a way to distingush between different `Rc`s of the same value?

て烟熏妆下的殇ゞ 提交于 2020-01-24 10:14:07
问题 Here's an example: use std::rc::Rc; #[derive(PartialEq, Eq)] struct MyId; pub fn main() { let rc_a_0 = Rc::new(MyId); let rc_a_1 = rc_a_0.clone(); let rc_b_0 = Rc::new(MyId); let rc_b_1 = rc_b_0.clone(); println!("rc_a_0 == rc_a_1: {:?}", rc_a_0 == rc_a_1); println!("rc_a_0 == rc_b_0: {:?}", rc_a_0 == rc_b_0); } Both println! s above print true . Is there a way distinguish between the rc_a_* and rc_b_* pointers? 回答1: You can cast &*rc to *const T to get a pointer to the underlying data and

Smart pointers as class members for polymorphism

三世轮回 提交于 2020-01-23 08:17:10
问题 I'm new to smart pointers and I would be really grateful if somebody could give me a hint whether the way I'm handling smart pointers as class members is correct. More precisely, the solution that I would like to achieve is in the context of class polymorphism and should be ideally exception-safe. Given a container of heterogeneuous objects ( std::vector<shared_ptr<CBase> > my_vector ), the usual way to add elements is: my_vector.push_back( shared_ptr<CBase>(new CChild(1))) , so that later on

Smart pointers as class members for polymorphism

怎甘沉沦 提交于 2020-01-23 08:17:06
问题 I'm new to smart pointers and I would be really grateful if somebody could give me a hint whether the way I'm handling smart pointers as class members is correct. More precisely, the solution that I would like to achieve is in the context of class polymorphism and should be ideally exception-safe. Given a container of heterogeneuous objects ( std::vector<shared_ptr<CBase> > my_vector ), the usual way to add elements is: my_vector.push_back( shared_ptr<CBase>(new CChild(1))) , so that later on