smart-pointers

Pushing an object with unique_ptr into vector in C++

一曲冷凌霜 提交于 2021-02-18 10:40:15
问题 I have a simple class structure modelling a discrete simulation, with a vector of States, which each contain a number of Transitions, held as a vector of smart pointers. I've used smart pointers to hold the transitions as in my full application I need polymorphism. #include <vector> #include <memory> class Transition { public: Transition() {} }; class State { public: State(int num) : num(num), transitions() {} void add_transition(std::unique_ptr<Transition> trans) { transitions.push_back(std:

Pushing an object with unique_ptr into vector in C++

可紊 提交于 2021-02-18 10:40:09
问题 I have a simple class structure modelling a discrete simulation, with a vector of States, which each contain a number of Transitions, held as a vector of smart pointers. I've used smart pointers to hold the transitions as in my full application I need polymorphism. #include <vector> #include <memory> class Transition { public: Transition() {} }; class State { public: State(int num) : num(num), transitions() {} void add_transition(std::unique_ptr<Transition> trans) { transitions.push_back(std:

Why does assignment operator call constructor?

天涯浪子 提交于 2021-02-16 15:55:28
问题 I am just playing around to understand smart pointers and trying to make mine but I come across a situation that I do not fully understand. Here is the code: #include <iostream> template <class T> class Holder { private: T * obj; public: Holder(T * tt) : obj(tt) { std::cout << "ctor : " << tt->dummy << std::endl; } T * operator -> () { return obj; } operator bool() { return obj; } T * const get() const { return obj; } void reset() {swap(0);} void swap(T * other) { obj = other; } Holder &

Smart pointer toolkit using variadic CRTP

只愿长相守 提交于 2021-02-11 12:52:28
问题 I am about to design and implement a kind of smart pointer toolkit - a set of classes to define various types of smart pointers like unique_ptr, intrusive_ptr, shared_ptr, observing_ptr, tagged_ptr etc. Just to mention I am working in freestanding environment where I have no c++ library available. My intersion is to avoid code duplications, make it follow an elegant design principle. Let me describe my thoughts in there. Design Considerations: I wanna use variadic CRTP approach to mixin the

How to pass a smart pointer to a function expecting a raw pointer?

梦想与她 提交于 2021-02-10 08:13:35
问题 I have the following code: unsigned char* frame_buffer_data{ new unsigned char[data_size] }; glReadPixels(origin_x, origin_y, width, height, GL_BGR, GL_UNSIGNED_BYTE, frame_buffer_data); I want to get rid of the raw pointer ( frame_buffer_data ) and use a unique pointer. Trying this: std::unique_ptr<unsigned char> framebuffer_data(new unsigned char[data_size] ); does not work. How can I pass the unique pointer (or other smart pointer) to this function? After the call to glReadPixels I need to

How to pass a smart pointer to a function expecting a raw pointer?

无人久伴 提交于 2021-02-10 08:10:21
问题 I have the following code: unsigned char* frame_buffer_data{ new unsigned char[data_size] }; glReadPixels(origin_x, origin_y, width, height, GL_BGR, GL_UNSIGNED_BYTE, frame_buffer_data); I want to get rid of the raw pointer ( frame_buffer_data ) and use a unique pointer. Trying this: std::unique_ptr<unsigned char> framebuffer_data(new unsigned char[data_size] ); does not work. How can I pass the unique pointer (or other smart pointer) to this function? After the call to glReadPixels I need to

How to pass a smart pointer to a function expecting a raw pointer?

大憨熊 提交于 2021-02-10 08:08:10
问题 I have the following code: unsigned char* frame_buffer_data{ new unsigned char[data_size] }; glReadPixels(origin_x, origin_y, width, height, GL_BGR, GL_UNSIGNED_BYTE, frame_buffer_data); I want to get rid of the raw pointer ( frame_buffer_data ) and use a unique pointer. Trying this: std::unique_ptr<unsigned char> framebuffer_data(new unsigned char[data_size] ); does not work. How can I pass the unique pointer (or other smart pointer) to this function? After the call to glReadPixels I need to

smart pointer to manage socket file descriptor

三世轮回 提交于 2021-02-08 07:14:39
问题 A smart pointer clears the memory if the pointer gets out of scope. I wanted to adapt this to a file descriptor, like a socket. There you need a user defined deleter, because close() is the function to free the file descriptor (fd) resources. I found this useful page, unfortunately, most approaches did not work for me. Below is a working solution I found up to now, which is a little nasty. Because uniqu_ptr expects a pointer I created int *fd to store the fd value, therefore, I had to close(

Passing the address of dereferenced smart pointers to functions that expect raw pointers

試著忘記壹切 提交于 2021-02-07 12:14:29
问题 (assuming that I am working with a library or framework that expects usage of raw pointers,) Is it valid practice to use a smart pointer which owns some data, and then pass the address of the derefenced smart pointer to a function that expects a raw pointer? 回答1: Yes, that is valid practice. The std smart pointers have a get() member function exactly for that purpose. In general, when you manage an object through smart pointers, you should only pass the whole smart-pointer-object as is to

Passing the address of dereferenced smart pointers to functions that expect raw pointers

 ̄綄美尐妖づ 提交于 2021-02-07 12:05:40
问题 (assuming that I am working with a library or framework that expects usage of raw pointers,) Is it valid practice to use a smart pointer which owns some data, and then pass the address of the derefenced smart pointer to a function that expects a raw pointer? 回答1: Yes, that is valid practice. The std smart pointers have a get() member function exactly for that purpose. In general, when you manage an object through smart pointers, you should only pass the whole smart-pointer-object as is to