smart-pointers

QList of QScopedPointers

我的未来我决定 提交于 2019-12-01 22:44:27
I'm trying to store QScopedPointers in a QList. I found this comment One can also use QList >. – Kuba Ober Jan 14 '14 at 18:17 (first comment on this answer: https://stackoverflow.com/a/21120575/3095014 ) and this post https://forum.qt.io/topic/59338/solved-qlist-of-qscopedpointers which implies that this should work. But if I try to compile the code of the second link, I'm getting this errors: E:\Qt\Qt5Enterprise\5.5\msvc2013\include\QtCore/qlist.h(404) : error C2248: 'QScopedPointer<Label,QScopedPointerDeleter<T>>::QScopedPointer' : cannot access private member declared in class

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

廉价感情. 提交于 2019-12-01 21:00:41
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; // destructor implemented in source file, so Page is an incomplete type }; template<class Visitor> inline

C++: Creating a shared object rather than a shared pointer to an object

我与影子孤独终老i 提交于 2019-12-01 19:30:00
boost::shared_ptr really bothers me. Certainly, I understand the utility of such a thing, but I wish that I could use the shared_ptr<A> as an A* . Consider the following code class A { public: A() {} A(int x) {mX = x;} virtual void setX(int x) {mX = x;} virtual int getX() const {return mX;} private: int mX; }; class HelpfulContainer { public: //Don't worry, I'll manager the memory from here. void eventHorizon(A*& a) { cout << "It's too late to save it now!" << endl; delete a; a = NULL; } }; int main() { HelpfulContainer helpfulContainer; A* a1 = new A(1); A* a2 = new A(*a1); cout << "*a1 = " <

Why doesn't shared_ptr permit direct assignment

若如初见. 提交于 2019-12-01 15:04:59
So when using shared_ptr<Type> you can write: shared_ptr<Type> var(new Type()); I wonder why they didn't allow a much simpler and better (imo): shared_ptr<Type> var = new Type(); Instead to achieve such functionality you need to use .reset() : shared_ptr<Type> var; var.reset(new Type()); I am used to OpenCV Ptr class that is a smart pointer that allows direct assignment and everything works fine The issue with allowing a raw pointer to be implicitly converted into a std::shared_ptr can be demonstrated with void foo(std::shared_ptr<int> bar) { /*do something, doesn't matter what*/ } int main()

Set shared_ptr with new_pointer that is old_pointer + offset

天大地大妈咪最大 提交于 2019-12-01 14:55:48
Here is a smart pointer: std::shared_ptr<char> p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header information (a few first dwords). Then actual data follows. Without giving much more context, it's handy for me to to set mentioned shared pointer to new address that is beginning of actual data . This address is still in alocated memory. But how to set without losing it? A question is (yes/no): Is it possible to set p to offset of prevous pointer,

Why doesn't shared_ptr permit direct assignment

懵懂的女人 提交于 2019-12-01 13:55:35
问题 So when using shared_ptr<Type> you can write: shared_ptr<Type> var(new Type()); I wonder why they didn't allow a much simpler and better (imo): shared_ptr<Type> var = new Type(); Instead to achieve such functionality you need to use .reset() : shared_ptr<Type> var; var.reset(new Type()); I am used to OpenCV Ptr class that is a smart pointer that allows direct assignment and everything works fine 回答1: The issue with allowing a raw pointer to be implicitly converted into a std::shared_ptr can

Set shared_ptr with new_pointer that is old_pointer + offset

南楼画角 提交于 2019-12-01 13:41:26
问题 Here is a smart pointer: std::shared_ptr<char> p(new char[size]) which represents array filled with raw binary file content. After (and only after) the whole array is copied from file to RAM, I can parse it, and during this I retrieve some header information (a few first dwords). Then actual data follows. Without giving much more context, it's handy for me to to set mentioned shared pointer to new address that is beginning of actual data . This address is still in alocated memory. But how to

cpp make_shared for void pointers

此生再无相见时 提交于 2019-12-01 13:33:22
I would like to use std::make_shared to create a void pointer. Since make_shared is supposed to be faster than shared_ptr(new T), and exception save I wonder if there is a library function to create a shared_ptr(new foo) in the make_shared way. You can convert any shared_ptr<foo> to shared_ptr<void> without the loss of efficiency associated with make_shared : #include <memory> struct foo {}; int main() { std::shared_ptr<void> p = std::make_shared<foo>(); } The conversion keeps the foo and the reference count in the same memory allocation, even though you now refer to it via a void* . Update

-> usage in smart pointers

烂漫一生 提交于 2019-12-01 11:22:22
I have a simple smart pointer implementation shown in code snippet 1 below. And a dummy test class named Dummy in the 2nd snippet. The code snippet 3 shows how we can utilize this simple smart pointer to access the function foo(). My question is about the way we invoke the function foo() in class Dummy by using the -> operator. -> operator already returns a pointer to the raw pointer. So, I think, in order for us to be able to invoke function foo(), we need to perform a second -> operation on the returned raw pointer. However, many resources say that a single use of the -> operator is

cpp make_shared for void pointers

戏子无情 提交于 2019-12-01 09:45:20
问题 I would like to use std::make_shared to create a void pointer. Since make_shared is supposed to be faster than shared_ptr(new T), and exception save I wonder if there is a library function to create a shared_ptr(new foo) in the make_shared way. 回答1: You can convert any shared_ptr<foo> to shared_ptr<void> without the loss of efficiency associated with make_shared : #include <memory> struct foo {}; int main() { std::shared_ptr<void> p = std::make_shared<foo>(); } The conversion keeps the foo