smart-pointers

How to make all copies of a shared_ptr equal to another shared_ptr?

不问归期 提交于 2020-01-02 18:34:09
问题 I cannot figure this out.. Looks like I'm missing something simple? What do I put in MakePointToSameValue so that at point (1) both b.ptr and c.ptr point to the same as a.ptr in other words, a.ptr.get() == b.ptr.get() == c.ptr.get() the value originally pointed to by b.ptr and c.ptr gets deleted ? struct Test { public: Test( int val ) : ptr( std::make_shared< int >( val ) ) { } void MakePointToSameValue( Test& other ) { //what do I put here? //other.ptr = this->ptr; //doesn't do it } private:

C++ “smart pointer” template that auto-converts to bare pointer but can't be explicitly deleted

落花浮王杯 提交于 2020-01-02 04:37:11
问题 I am working in a very large legacy C++ code base which shall remain nameless. Being a legacy code base, it passes raw pointers around all over the place. But we are gradually trying to modernize it and so there are some smart pointer templates as well. These smart pointers (unlike, say, Boost's scoped_ptr) have an implicit conversion to the raw pointer, so that you can pass one of them into a routine that takes a raw pointer without having to write .get() . A big downside of this is that you

Undefined reference error when initializing unique_ptr with a static const

江枫思渺然 提交于 2020-01-02 01:58:21
问题 When I try to use a static const to initialize a unique_ptr , I get an "undefined reference" error. However, when I new a pointer using the same constant, the symbol seems to be magically defined. Here is a simple program that reproduces the error: Outside_library.h class Outside_library { public: static const int my_const = 100; }; main.cpp #include "Outside_library.h" #include <iostream> #include <memory> class My_class { public: My_class(int num) { m_num = num; }; virtual ~My_class(){};

Smart pointers in Qt [duplicate]

旧巷老猫 提交于 2020-01-02 00:13:46
问题 This question already has answers here : What C++ Smart Pointer Implementations are available? (3 answers) Closed 6 years ago . Like it has been written here Qt up to now has 8 specilized smart pointer classes. It looks like it is all you will ever need. However, in order to use any of these smart pointers your class must be derived from QObject which is not always convenient. Is there other implementations of smart pointers in Qt which work with arbitrary classes? 回答1: Many Qt classes are

How to assign the address of an existing object to a smart pointer?

霸气de小男生 提交于 2020-01-01 23:58:07
问题 #include <memory> class bar{}; void foo(bar &object){ std::unique_ptr<bar> pointer = &object; } I want to assign an address of the object to the pointer. The above code obviously wont compile, because the right side of the assignment operator needs to be a std::unique_ptr. I've already tried this: pointer = std::make_unique<bar>(object) But it throws many errors during compilation. How can I do that? Update As said in the answers - using the std::unique_ptr::reset method led to undefined

Incomplete type for std::vector

廉价感情. 提交于 2020-01-01 04:34:06
问题 The GCC compiler complains (see below) when I try the following. class Face needs to be incomplete because it contains pointer to class Element which similarly contains pointer to class Face . In other words, there is a circular dependency among classes. How can I fix it? error: invalid application of ‘sizeof’ to incomplete type ‘Face’ class Face; // needs to be incomplete class Element { std::vector < std::unique_ptr <Face> > face; }; class Face { std::vector < std::unique_ptr <Element> >

Shared void pointers. Why does this work?

余生长醉 提交于 2019-12-29 05:04:11
问题 To solve a very peculiar problem in my application I need a shared-pointer to allocated data, but to the outside world, the underlying data type should remain hidden. I could solve this by making some kind of Root class of which all my other classes inherit, and use a shared_ptr on this Root class, like this: std::shared_ptr<Root> However: I don't want all my classes to inherit from this Root class just to be able to have this shared pointer Sometimes I want to return a shared pointer to std:

smart pointers + “this” considered harmful?

人走茶凉 提交于 2019-12-29 04:02:45
问题 In a C++ project that uses smart pointers, such as boost::shared_ptr , what is a good design philosophy regarding use of " this "? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up control of object deletion and trust the smart pointer to do it at the right time. Non-static class members intrinsically use a this pointer. It's a raw pointer and that can't be changed. If I ever store this in another variable or pass it to

smart pointers + “this” considered harmful?

偶尔善良 提交于 2019-12-29 04:02:24
问题 In a C++ project that uses smart pointers, such as boost::shared_ptr , what is a good design philosophy regarding use of " this "? Consider that: It's dangerous to store the raw pointer contained in any smart pointer for later use. You've given up control of object deletion and trust the smart pointer to do it at the right time. Non-static class members intrinsically use a this pointer. It's a raw pointer and that can't be changed. If I ever store this in another variable or pass it to

Should I delete the move constructor and the move assignment of a smart pointer?

不羁岁月 提交于 2019-12-29 03:04:46
问题 I'm implementing a simple smart pointer, which basically keeps track of the number of references to a pointer that it handles. I know I could implement move semantics, but I don't think it makes sense as copying a smart pointer is very cheap. Especially considering that it introduces opportunities to produce nasty bugs. Here's my C++11 code (I omitted some inessential code). General comments are welcome as well. #ifndef SMART_PTR_H_ #define SMART_PTR_H_ #include <cstdint> template<typename T>