Skip to content

Commit 04fed30

Browse files
committed
unique_ptr++
1 parent 29302f7 commit 04fed30

1 file changed

Lines changed: 12 additions & 4 deletions

File tree

‎cpp/unique_ptr.cpp‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -151,20 +151,28 @@ int main() {
151151
assert(Base::count == 0);
152152
}
153153

154-
// There is no assignment operator, but reset with an argument
154+
// Reset with an argument
155155
// does what you would expect: release and replace.
156156
{
157157
std::unique_ptr<Base> p(new Base(1));
158158
assert(p->i == 1);
159159
assert(Base::count == 1);
160160

161-
// Nope.
162-
// p = new Base(2);
161+
/* Nope. TODO why?
162+
* - https://stackoverflow.com/questions/48104034/why-cant-i-assign-to-unique-ptr-of-type-uint-8
163+
* - https://stackoverflow.com/questions/34882140/why-cant-a-pointer-be-automatically-converted-into-a-unique-ptr-when-returning
164+
*/
165+
//p = new Base(2);
163166

164167
// Yup.
165-
p.reset(new Base(2));
168+
p = std::unique_ptr<Base>(new Base(2));
166169
assert(p->i == 2);
167170
assert(Base::count == 1);
171+
172+
// Yup, same. Less verbose, I like it.
173+
p.reset(new Base(3));
174+
assert(p->i == 3);
175+
assert(Base::count == 1);
168176
}
169177
assert(Base::count == 0);
170178
}

0 commit comments

Comments
 (0)