Book Image

Learning C++ by creating games with UE4

By : William Sherif
Book Image

Learning C++ by creating games with UE4

By: William Sherif

Overview of this book

Table of Contents (19 chapters)
Learning C++ by Creating Games with UE4
Credits
About the Author
About the Reviewers
www.PacktPub.com
Preface
2
Variables and Memory
Index

Chapter 9. Templates and Commonly Used Containers

In Chapter 7, Dynamic Memory Allocation, we spoke about how you will use dynamic memory allocation if you want to create a new array whose size isn't known at compile time. Dynamic memory allocations are of the form int * array = new int[ number_of_elements ].

You also saw that dynamic allocations using the new[] keyword require you to call delete[] on the array later, otherwise you'd have a memory leak. Having to manage memory this way is hard work.

Is there a way to create an array of dynamic size and have the memory automatically managed for you by C++? The answer is yes. There are C++ object types (commonly called containers) that handle dynamic memory allocations and deallocations automatically. UE4 provides a couple of container types to store your data in dynamically resizable collections.

There are two different groups of template containers. There is the UE4 family of containers (beginning with T*) and the C++ Standard Template Library...