smart-pointers

How to implement a wrapper around C 'objects' using smart pointers?

两盒软妹~` 提交于 2020-08-08 06:33:05
问题 I am using a C library which uses raw pointers from C++. Therefore, I'm looking into wrapping all the pointers to C objects in C++ classes and turning them into smart pointers. I've built a working example: #include <iostream> using namespace std; // the C library is oop: using structs and naming conventions. Like this: // C library declarations struct Animal_s { int age; }; typedef struct Animal_s Animal; Animal* make_animal(int age); void free_animal(Animal* animal); Animal* do_something

How to implement a wrapper around C 'objects' using smart pointers?

百般思念 提交于 2020-08-08 06:32:39
问题 I am using a C library which uses raw pointers from C++. Therefore, I'm looking into wrapping all the pointers to C objects in C++ classes and turning them into smart pointers. I've built a working example: #include <iostream> using namespace std; // the C library is oop: using structs and naming conventions. Like this: // C library declarations struct Animal_s { int age; }; typedef struct Animal_s Animal; Animal* make_animal(int age); void free_animal(Animal* animal); Animal* do_something

How to pattern match on values inside a type implementing Deref, such as Box, without copying the contents?

荒凉一梦 提交于 2020-07-09 07:30:30
问题 I have data contained inside a Box , and would like to pattern match on it without accidentally copying the Box 's contents from the heap to the stack; how do I do that? Let's assume the following code: enum SomeEnum { SomeEntry, AnotherEntry, } fn main() { let boxed_value = Box::new(SomeEnum::AnotherEntry); match *boxed_value { SomeEnum::SomeEntry => {} SomeEnum::AnotherEntry => {} } } Does this copy the enum out of the box onto the stack and pattern match on that copy, or does it do the

About “circular reference”, I used weak_ptr but memory leak still happened

一笑奈何 提交于 2020-06-26 07:42:32
问题 I read: How to avoid memory leak with shared_ptr? I know that I need to use weak_ptr to avoid circular reference . So I created a little program to play circular reference . Following object(spyder) will be invoke class spyder { public: spyder(std::string _name): m_name(_name), finger(nullptr) { } inline const std::string ask_name() const{ return m_name; } std::shared_ptr<spyder> finger; private: std::string m_name; }; I invoke spyder in my main code with shared_ptr and weak_ptr: int main(){

How to access value in RefCell properly

老子叫甜甜 提交于 2020-05-27 04:43:00
问题 I'm trying to wrap my head around Rc and RefCell in Rust. What I'm trying to achieve is to to have multiple mutable references to the same objects. I came up with this dummy code: use std::rc::Rc; use std::cell::RefCell; struct Person { name: String, mother: Option<Rc<RefCell<Person>>>, father: Option<Rc<RefCell<Person>>>, partner: Option<Rc<RefCell<Person>>> } pub fn main () { let mut susan = Person { name: "Susan".to_string(), mother: None, father: None, partner: None }; let mut boxed_susan

How to access value in RefCell properly

牧云@^-^@ 提交于 2020-05-27 04:42:31
问题 I'm trying to wrap my head around Rc and RefCell in Rust. What I'm trying to achieve is to to have multiple mutable references to the same objects. I came up with this dummy code: use std::rc::Rc; use std::cell::RefCell; struct Person { name: String, mother: Option<Rc<RefCell<Person>>>, father: Option<Rc<RefCell<Person>>>, partner: Option<Rc<RefCell<Person>>> } pub fn main () { let mut susan = Person { name: "Susan".to_string(), mother: None, father: None, partner: None }; let mut boxed_susan

Factory pattern using unique_ptr in c++

China☆狼群 提交于 2020-05-25 05:31:15
问题 I have an old factory implementation in c++, and I want to use unique pointers instead of raw pointers in it. A minimal example of my code is as follows. I have a base class A , and a derived class B . In main() , I pass 1 to the create method in A , and the type of b1 is now changed to B . #include <iostream> #include <map> class A { public: A() {} virtual void Foo() {} std::map<int, A *> &registerType() { static std::map<int, A *> map_instance; return map_instance; } A *create(int n) {

Factory pattern using unique_ptr in c++

允我心安 提交于 2020-05-25 05:31:10
问题 I have an old factory implementation in c++, and I want to use unique pointers instead of raw pointers in it. A minimal example of my code is as follows. I have a base class A , and a derived class B . In main() , I pass 1 to the create method in A , and the type of b1 is now changed to B . #include <iostream> #include <map> class A { public: A() {} virtual void Foo() {} std::map<int, A *> &registerType() { static std::map<int, A *> map_instance; return map_instance; } A *create(int n) {

When should we use std::enable_shared_from_this

六月ゝ 毕业季﹏ 提交于 2020-04-05 15:06:49
问题 I just knew std::enable_shared_from_this form this link. But after reading the code below, I don't know when to use it. try { Good not_so_good; std::shared_ptr<Good> gp1 = not_so_good.getptr(); } catch(std::bad_weak_ptr& e) { // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) std::cout << e.what() << '\n'; } The code above is "not so good" because there is no existing shared_ptr before calling getptr() . So the good thing should be: std::shared_ptr<Good> gp1 = std:

When should we use std::enable_shared_from_this

拈花ヽ惹草 提交于 2020-04-05 15:06:03
问题 I just knew std::enable_shared_from_this form this link. But after reading the code below, I don't know when to use it. try { Good not_so_good; std::shared_ptr<Good> gp1 = not_so_good.getptr(); } catch(std::bad_weak_ptr& e) { // undefined behavior (until C++17) and std::bad_weak_ptr thrown (since C++17) std::cout << e.what() << '\n'; } The code above is "not so good" because there is no existing shared_ptr before calling getptr() . So the good thing should be: std::shared_ptr<Good> gp1 = std: