Difference between revisions of "Intermediate C++ Game Programming Tutorial 21"
(→Video Timestamp Index) |
(→Video Timestamp Index) |
||
(7 intermediate revisions by the same user not shown) | |||
Line 48: | Line 48: | ||
</div> | </div> | ||
* Implementing smart pointers in the MemeFighter game [https://youtu.be/WCTiFVlQFZU?t=13m58s 13:58] | * Implementing smart pointers in the MemeFighter game [https://youtu.be/WCTiFVlQFZU?t=13m58s 13:58] | ||
+ | <div class="mw-collapsible-content"> | ||
** Use <code>std::vector<std::unique_ptr<MemeFighter>> team;</code> to represent the teams of MemeFighters | ** Use <code>std::vector<std::unique_ptr<MemeFighter>> team;</code> to represent the teams of MemeFighters | ||
** Use <code>team.emplace_back(...)</code> to add elements to the vector, as you cannot use <code>= {.. , .. , ..}</code> to initialize a vector with unique pointers | ** Use <code>team.emplace_back(...)</code> to add elements to the vector, as you cannot use <code>= {.. , .. , ..}</code> to initialize a vector with unique pointers | ||
Line 53: | Line 54: | ||
** Rewrite the ownership of a weapon to be represented by <code>std::unique_ptr<Weapon> pWeapon</code> | ** Rewrite the ownership of a weapon to be represented by <code>std::unique_ptr<Weapon> pWeapon</code> | ||
** Remember to pass unique pointers by value when you want to transfer ownership inside a function | ** Remember to pass unique pointers by value when you want to transfer ownership inside a function | ||
− | * Just because you work with smart pointers, doesn't mean that everything you pass into functions has to be passed in with a smart pointer | + | </div> |
+ | * When to pass smart pointers in function calls [https://youtu.be/WCTiFVlQFZU?t=17m47s 17:47] | ||
+ | <div class="mw-collapsible-content"> | ||
+ | ** Just because you work with smart pointers, doesn't mean that everything you pass into functions has to be passed in with a smart pointer | ||
** The only time you should be passing in smart pointers is when you are transfering ownership inside the function | ** The only time you should be passing in smart pointers is when you are transfering ownership inside the function | ||
− | ** Otherwise, just pass in with a normal reference to the object | + | ** Otherwise, just pass in with a normal reference to the object (or a normal pointer sometimes) |
− | * | + | ** One exception here are functors for std <algorithm>s, see above |
+ | </div> | ||
+ | * Implementing smart pointers in the Surface class used for sprite loading & drawing [https://youtu.be/WCTiFVlQFZU?t=18m52s 18:52] | ||
+ | * Going over the documentation for <code>unique_ptr</code> on cppreference.com [https://youtu.be/WCTiFVlQFZU?t=21m55s 21:55] | ||
+ | * Review of main lessons [https://youtu.be/WCTiFVlQFZU?t=25m23s 25:23] | ||
</div> | </div> | ||
Latest revision as of 05:02, 3 November 2019
Smart pointers. I know pointers, I have the smartest pointers. In this video we learn about std::unique_ptr<>
, which is by far the most frequently-used and important smart pointer. std::shared_ptr<>
can go suck on an egg.
Contents
[hide]Topics Covered
-
std::unique_ptr<>
- How to pass (and not to pass) smart pointers
- Custom deleters
Video Timestamp Index
- Main risk factors for leaking memory when using pointers and (dynamically allocated) heap memory 0:30
- Smart pointers / unique pointers can help avert these inadvertant memory leaks 2:39
- Creating unique pointer in modern C++:
std::unique_ptr<T> p = std::make_unique<T>()
3:57
- Example: functions in a class to transfer ownership of a resource through smart pointers 7:00
- You control ownership of the resource object by using move semantics 9:12
- Using a factory pattern (creating a factory function) to create unique pointers 11:47
- Implementing smart pointers in the MemeFighter game 13:58
- When to pass smart pointers in function calls 17:47
Bonus Video
The addition of smart pointers to Surface
has allowed us to reduce the amount of code we need to write for Surface
, making it simpler and correct by default. But we still need to write logic for those pesky copy operations... What if we used a container like std::vector
! Then all that copying bullshit will be taken care of for us.
This is what we do in the bonus video. The only problem is, although adding std::vector
will not make our release build any slower (the compiler optimizes out all of the extra abstraction for us), it does make the debug build slow. Usually, we don't worry about performance under debug, but for stuff like PutPixel
, since we're calling it potentially millions of times per second, if our debug build is too slow it will become unusable for development and testing.
So the second goal of this bonus video is to optimize the debug build so that PutPixel
-related operation run very fast, regardless of the fact that we are using std::vector
to manage the array of pixels. The end result is that we can achieve rendering speeds which are over 100x faster than without the optimized debug configuration.
Notes
Although Chili uses std::unique_ptr<>
in Surface
, which allows us to = default
the move members and leave the destructor undeclared, the astute student will realize that Surface
should actually be setting the width
and height
of the donor surface to 0
when pilfering, so we actually still need to declare move members in this case. This is done in the Bonus Video where we perfect Surface
.
Also note one major mistake: when we convert MemeFighter
to use std::unique_ptr
for owning its Weapon
, we remove the destructor (because we no longer need to manually delete
the heap Weapon
object). However, we still need to mark the destructor as virtual
so that the proper derived destructor gets called in polymorphic situations. The correct course should have been to change the destructor to virtual ~MemeFighter() = default;
(a commit has been pushed after the fact that fixes this issue).