Questions tagged [unique-ptr]

std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer. unique_ptr is not copyable or copy-assignable, two instances of unique_ptr cannot manage the same object.

cppreference:

std::unique_ptr is a smart pointer that retains sole ownership of an object through a pointer and destroys that object when the unique_ptr goes out of scope. No two unique_ptr instances can manage the same object.

std::unique_ptr was designed to replace the std::auto_ptr in C++03. It improves on the implementation of auto_ptr by implementing specific move semantics (it is not copyable) that were not available in the language of C++03.

std::unique_ptr, together with std::shared_ptr and (std::weak_ptr) form the core smart pointers used in C++ to implement RAII semantics, especially with respect to traditional memory management. With custom deleters, these smart pointers can also be used to manage other resources.

Resources:

2193 questions
1
vote
2 answers

Transferring Ownership in vector of unique_ptrs

I have 2 classes A and B //A.h class A{}; // B.h typedef unique_ptr APtr; typedef vector BVEC; class B { public: BVEC vec; //error is here //.... }; When I compile the code I get unique_ptr....attempting to reference a…
1
vote
2 answers

Vector of unique_ptr member

I have the following: typedef std::vector> NodeList; class Node { public: Node(); Node(NodeType _type); virtual ~Node(); NodeType getNodeType() const; Node const* getParentNode() const; // I want a…
gosom
  • 1,299
  • 3
  • 16
  • 35
1
vote
2 answers

Concise notation for assigning `unique_ptr`?

I have a pointer to a parent class and I want to assign a new child object to that pointer conditionally. Right now, the syntax I have is rather lengthly: std::unique_ptr parentPtr; if (...) { parentPtr =…
nbubis
  • 2,304
  • 5
  • 31
  • 46
1
vote
1 answer

How can I find the source of a copy?

I have some container-using code that is causing me grief. The issue is that I want to put a smart pointer (unique_ptr) in one of the subfields. Eg: struct subrecord { int id; int handle; …
T.E.D.
  • 44,016
  • 10
  • 73
  • 134
1
vote
4 answers

how to pop_back shared pointer from vector and convert to unique_ptr

I'm trying to pop_back my shared_pointer from my vector and convert to a unique_ptr. Unfortunately, it's giving a strange compilation message. IFCCB.cpp: std::unique_ptr IFCCCB::getElementVectorIFC() { return…
Michele
  • 3,617
  • 12
  • 47
  • 81
1
vote
2 answers

How push_back unique_ptr parameter onto vector of shared ptrs

I'm having a tough time pushing back a unique_ptr from my method parameter onto a vector of shared pointers. IFCCB.h: private: vector> m_shpVectorIFC; public: void addElementVectorIFC(unique_ptr rupIFC); IFCCB.cpp: void…
Michele
  • 3,617
  • 12
  • 47
  • 81
1
vote
1 answer

unique_ptr in class how to work with them

I am implementing AVL tree in C++ and using unique_ptr for children. struct Node { const int key; std::unique_ptr left, right; Node* parent; std::size_t height; ///< for avl tree. Node(const int key) : key(key), height(0)…
Ashish Negi
  • 5,193
  • 8
  • 51
  • 95
1
vote
3 answers

How to add objects to a std::vector>?

Question: How to add objects to a std::vector>? I have a class, and this is what I am trying to do... Below, I'm trying to use std::unique_ptr in my vector, as I thought it would be the easiest. class Ball { public: …
Andreas DM
  • 10,685
  • 6
  • 35
  • 62
1
vote
2 answers

Seg. fault with std::unique_ptr and ctor

For a parser I am actually implementing I partially have these private functions within the parser: Parser private methods: Token const* current_token() const; Token const* next_token(); Token const* peek_token(); …
1
vote
2 answers

c++ passing ptrs around

Still having lots of problems grasping pointers, i've tried to not use them, but i'm not sure how to best accomplish the following. I have two classes Point and Ray, I'm also sure that there is probably already an implementation of a point class…
Evan Ward
  • 1,371
  • 2
  • 11
  • 23
1
vote
3 answers

c++ swapping unique_ptr's

New to this kind of stuff, probably doing something wrong, but - I have 3 members std::unique_ptr currentWeapon; std::unique_ptr weaponSlotOne; std::unique_ptr weaponSlotTwo; Gun is a base class that has other derived classes such as…
Evan Ward
  • 1,371
  • 2
  • 11
  • 23
1
vote
1 answer

Why can't I use unique_ptr with fread?

If you run the code below fread will return with 0. If you change p to use buf instead of the unique_ptr it will work. Why? I ran this in MSVC 2013 #include #include #include using namespace std; int main(int argc, char…
user34537
1
vote
2 answers

Resetting nested smart pointer of a shared_ptr to a shared_ptr (or to a unique_ptr), seeming paradox

I know the object managed by a std::shared_ptr is not deleted by reset() unless it is the only shared_ptr that manages it at that point. I know that when there are multiple shared_ptrs managing the same object, changes to the managed object’s value…
CodeBricks
  • 1,771
  • 3
  • 17
  • 37
1
vote
2 answers

make_unique giving error 2248

I am having an issue with make_unique that I am at a loss with. _replace_find = unique_ptr(new Fl_Input{ 80, 10, 210, 25, "Find:" }); _replace_find = make_unique(Fl_Input{ 80, 10, 210, 25, "Find:" }); when I use the make_unique…
Jason Ball
  • 163
  • 8
1
vote
2 answers

Memory leaks with recursive function using std::unique_ptr

I haven't used a std::unique_ptr before, so this is kind of my first attempt to trying to use it in recursion call as following: #define CRTDBG_MAP_ALLOC #include #include #include struct S { S(int X = 0, int Y =…
cpx
  • 17,009
  • 20
  • 87
  • 142