C++: Difference between revisions

457 bytes added ,  3 February 2020
Line 186: Line 186:
auto my_car = std::make_unique<Car>();
auto my_car = std::make_unique<Car>();
</syntaxhighlight>
</syntaxhighlight>
Note: If the object you need is not very large, you can consider just including it as part of your class (or leaving it on the stack) rather than use pointers.
;Notes
* If the object you need is not very large, you can consider just including it as part of your class (or leaving it on the stack) rather than use pointers.
* If you want to get a copy of the smart pointer to the current object, the object must '''publically''' inherit <code>std::enable_shared_from_this<T></code>
** Then you can call <code>shared_from_this()</code> from within any method (not the constructor).
** May throw <code>bad_weak_ptr</code> if you call <code>shared_from_this()</code> without <code>make_shared</code> or if you do not publically inherit <code>std::enable_shared_from_this<T></code>


====Garbage Collection====
====Garbage Collection====