Struct Constructors

Should You Use struct in C++?

A struct and a class in C++ differ mainly in default access. Here is when a struct fits, when a class is better and how constructors fit into the choice.

On this page
  1. The real difference
  2. When a struct fits
  3. When a class fits better
  4. Constructors do not decide it
  5. Be consistent

Use a struct when the type is mostly a bundle of values that code may read and change directly. Use a class when the type needs to protect its data or keep it in a valid state. Both can do either job, so the choice is about intent and convention rather than what the compiler allows.

The real difference

In C++, a struct and a class work the same way except for two default settings:

  • Members of a struct are public by default. Members of a class are private by default.
  • A base class listed without an access keyword is inherited publicly by a struct and privately by a class.

If you have asked “Is struct private or public by default?”, the answer is public. You can override that default with an explicit public: or private: label.

struct Pair {
    int first;   // public by default
    int second;
};

class Hidden {
    int secret;  // private by default
public:
    Hidden(int value) : secret(value) {}
};

When a struct fits

A struct is a good fit when the type just groups values:

  • A point, a color or a date, where values such as x and y, or red, green and blue, always travel together.
  • A result with several values that a function returns.
struct Point {
    int x;
    int y;
};

Point add(Point a, Point b) {
    return {a.x + b.x, a.y + b.y};
}

Callers can read x and y directly, and there is nothing to protect.

When a class fits better

Use a class when some values must follow rules. In this example, the balance is private, so the only way to change it is through deposit and withdraw, and both check their input:

class Account {
public:
    explicit Account(double opening) : balance_(opening) {}

    void deposit(double amount) {
        if (amount > 0) {
            balance_ += amount;
        }
    }

    bool withdraw(double amount) {
        if (amount <= 0 || amount > balance_) {
            return false;
        }
        balance_ -= amount;
        return true;
    }

    double balance() const {
        return balance_;
    }

private:
    double balance_;
};

Hiding data behind a small set of functions is the core of encapsulation in c, and it is the main reason to choose a class here.

Constructors do not decide it

Both a struct and a class can have constructors, member functions and base classes, so a constructor does not make a type a class. See c struct constructor for how constructors work on a struct, and c class constructor for the class side.

Some style guides recommend a simple rule: a struct for plain data with no behavior, and a class for anything else. That rule gives a clear answer in most cases. For a fuller comparison of the two, see c struct vs class.

Be consistent

Pick one convention for a project and stick to it, so readers can tell what kind of type they are looking at. The compiler accepts either.

The complete guide to struct constructors

Struct Constructors in C++
Read the guide

Keep exploring

Guides from other topics

All topics →

SOLID Principles in C++

Learn the five SOLID principles in C++ with short examples of single responsibility, open/closed, Liskov, interface segregation and dependency inversion.