In some of our class examples, we suggested that our objects should have properties like WorldPosition
- where that character currently exists in the world.
We could store this as 3 separate properties, eg, an x, y and z co-ordinate. But for situations like this, it is much better to package these things together, as a single object.
This means we need to create a new custom type of data. We've already seen how to do that using a class
, but there is an alternative - something called a structure, or struct
struct Vector3 {
float x;
float y;
float z;
}
class Character {
Vector3 Location { 0.0f, 0.0f, 0.0f };
};
Vector3
?A "vector" is a concept from maths and physics. Vectors are the most common way of representing a position within a space.
A custom Vector3
type (sometimes abbreviated to Vec3
) is typically how this is done within graphics applications.
The 3 in the type name indicates it is a 3-dimensional vector, used to store a position in a 3D environment.
We go into this concept in more detail in the next chapter.
How could we create a struct that represents a combat ability?
You may be thinking this problem could also be solved by classes. This is completely true - Vector3
could indeed be a class.
In C++, structs and classes are almost identical.
Structs once existed to solve these simple data requirements when we didn't need all the power of a class. However, as the language evolved over time, structs have become more powerful.
They are now just as powerful as classes. Structs can have functions, constructors, inheritance, and everything else that a class has.
The only difference between a struct and a class is that, by default, members of a struct
are public
whilst in a class
, they are private
.
However, the C++ community still attaches semantic differences to the two concepts. Even though they're technically almost identical, their usage is commonly different.
Google's style guide offers a good example, and many developers / companies have a similar approach:
structs
should be used for passive objects that carry data, but lack any functionality other than access/setting the data members.Initialize()
,
Reset()
.class
is more appropriate.class
.Moreover, the difference between classes and structs may become slightly more important when working within the context of other ecosystems, such as a game engine.
For example, in Unreal, structs have legitimate technical limitations that makes their differences with classes much more significant than what C++ has by default.
What is the main difference between a struct and a class?
Previously, we've seen how built in objects like int
and bool
allowed us to use operators like +
and >=
to interact with them.
It would be useful if we had a way to define operators for our custom types too. For example, we'll need the ability to add two of our vectors together.
We could define a function on our struct to facilitate that. It could allow us to add vectors together by doing something like this:
MyFirstVector.Add(MySecondVector);
But it would be nice if we could just do it like this instead:
MyFirstVector + MySecondVector;
In the next lesson, we can see how we can update our classes and structs to make these types of interactions possible.
Become a software engineer with C++. Starting from the basics, we guide you step by step along the way