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

struct objects


An object in C++ is basically any variable type that is made up of a conglomerate of simpler types. The most basic object in C++ is struct. We use the struct keyword to glue together a bunch of smaller variables into one big variable. If you recall, we did introduce struct briefly in Chapter 2, Variables and Memory. Let's revise that simple example:

struct Player
{
  string name;
  int hp;
};

This is the structure definition for what makes a Player object. The player has a string for his name and an integer for his hp value.

If you'll recall from Chapter 2, Variables and Memory, the way we make an instance of the Player object is like this:

Player me;    // create an instance of Player, called me

From here, we can access the fields of the me object like so:

me.name = "Tom";
me.hp = 100;

Member functions

Now, here's the exciting part. We can attach member functions to the struct definition simply by writing these functions inside the struct Player definition.

struct Player
{
  string...