C++ Structs vs Classes

An introduction to structs - an alternative to classes that provides another way to define our own data types.
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

3D art showing a female character
Ryan McCombe
Ryan McCombe
Posted

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 };
};

What is a 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.

Test your Knowledge

How could we create a struct that represents a combat ability?

C++ Structs vs Classes

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.
  • All fields must be public, and accessed directly rather than through getter/setter methods.
  • Methods should not provide behaviour but should only be used to set up the data e.g., constructor, Initialize(), Reset().
  • If more functionality is required, a class is more appropriate.
  • If in doubt, make it a 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.

Test your Knowledge

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.

Was this lesson useful?

Ryan McCombe
Ryan McCombe
Posted
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Namespaces, Enums and Structs
3D art showing a progammer setting up a development environment
This lesson is part of the course:

Intro to C++ Programming

Become a software engineer with C++. Starting from the basics, we guide you step by step along the way

Free, unlimited access!

This course includes:

  • 66 Lessons
  • Over 200 Quiz Questions
  • Capstone Project
  • Regularly Updated
  • Help and FAQ
Next Lesson

C++ Operator Overloading

Learn how we can enhance our classes and structs by letting them define their our own operators.
3D art showing a fantasy character
Contact|Privacy Policy|Terms of Use
Copyright © 2023 - All Rights Reserved