Struct Constructors

Struct Constructors in C++

Learn how C++ struct constructors set up members, with examples of default, parameterized and copy constructors, initializer lists and common mistakes.

On this page
  1. What a struct constructor does
  2. How the initializer list works
  3. The default constructor
  4. Braces without a constructor
  5. Explicit constructors
  6. Copy and move constructors
  7. Common mistakes
  8. Structs in C
  9. Structs and classes
  10. C++20 designated initializers
  11. When to add a constructor

What a struct constructor does

A constructor is a special member function that runs automatically when an object is created. It has the same name as the struct and no return type, not even void. A C++ struct can have constructors, and they work the same way they do in a class.

Here is a struct with one:

#include <iostream>

struct Point {
    int x;
    int y;

    Point(int x_value, int y_value) : x(x_value), y(y_value) {}
};

int main() {
    Point p(3, 4);  // calls the constructor
    std::cout << p.x << ", " << p.y << "\n";  // prints 3, 4
}

Point p(3, 4); creates the object and passes 3 and 4 to the constructor. The constructor body is empty here because the work happens in the initializer list, which is explained next.

How the initializer list works

The part between the colon and the braces is the member initializer list. Each member is set up before the constructor body runs. For int members, assigning inside the body gives the same result:

Point(int x_value, int y_value) {
    x = x_value;
    y = y_value;
}

The difference shows up with members of class types such as std::string, which are created first and then assigned. It also matters for const and reference members, because they can only be set in the initializer list.

Members are initialized in the order they are declared in the struct, not in the order they appear in the list. This matters when one member depends on another:

struct Square {
    int side;
    int area;

    Square(int s) : area(side * side), side(s) {}  // wrong: side is read before it is set
};

Reading side before it has a value is undefined behavior. Listing the members in declaration order fixes the problem:

struct Square {
    int side;
    int area;

    Square(int s) : side(s), area(s * s) {}
};

Many compilers warn about a mismatched list when warnings are turned on, so it is worth enabling them.

The default constructor

A default constructor takes no arguments. Two rules decide whether a struct has one:

  • If you declare no constructors, the compiler provides a default constructor.
  • If you declare any constructor, the compiler does not provide one. With the Point above, Point p; does not compile.

A default constructor does not mean the members are zeroed. Consider a struct with no constructors:

struct Point {
    int x;
    int y;
};

int main() {
    Point a;    // x and y hold unknown values
    Point b{};  // x and y are 0
}

A local variable of a built-in type such as int is not set to zero just because it is declared. Empty braces, as in Point b{};, set each member to zero.

To give a member a starting value without writing a constructor, use a default member initializer, which C++11 added:

struct Counter {
    int count = 0;
};

int main() {
    Counter c;  // c.count is 0
}

Braces without a constructor

A plain struct with no declared constructors can be initialized with a brace list. C++ calls such a type an aggregate:

struct Point {
    int x;
    int y;
};

int main() {
    Point p{3, 4};  // p.x is 3, p.y is 4
}

Once you declare a constructor, the struct is no longer an aggregate. Braces then call your constructor, so Point p{3, 4}; still works if the constructor takes two ints.

Explicit constructors

A constructor that takes one argument can be used for implicit conversions, which can surprise readers. Marking it explicit stops those conversions:

#include <iostream>

struct Meters {
    double value;

    explicit Meters(double v) : value(v) {}
};

void show(Meters m) {
    std::cout << m.value << " m\n";
}

int main() {
    // show(2.5);       // does not compile: 2.5 is not converted to Meters implicitly
    show(Meters(2.5));  // OK, prints 2.5 m
}

Copy and move constructors

Copying a struct uses the copy constructor. Moving from a temporary, or from a value passed to std::move, uses the move constructor. You do not have to write either one. The compiler generates them, and for a struct that holds only numbers, the generated versions copy each member.

Write your own copy or move constructor only when the struct manages a resource such as heap memory. Otherwise the generated versions are usually correct and simpler.

Common mistakes

  • Writing Point p(); declares a function named p that takes no arguments and returns a Point. It does not create an object. Write Point p{}; or pass arguments, as in Point p(3, 4);.
  • Reading a member before it has been set, as in the Square example with the wrong list order.
  • Expecting Point p; to keep working after you add a constructor that takes arguments. The compiler rejects it, which is the intended behavior.

Structs in C

C has structs but no constructors. C code usually sets members with a function that receives a pointer to the struct. Readers who ask “How do I create a struct in C?” will find that approach in the C-focused article. C++ constructors do that setup job automatically when the object is created.

Structs and classes

The same constructor syntax works in a class, so if you are reading about c class constructor, most of this carries over. Structs and classes differ mainly in default access, so the choice between them depends on how much you want hidden. The c struct vs class comparison has the details.

C++20 designated initializers

For aggregates in C++20, the brace list can name each member, as in Point p{.x = 3, .y = 4};. The names must follow the declaration order, and the syntax works only on aggregates, not on a struct with a constructor. The c designated initializers material covers the rules in more depth.

When to add a constructor

Add a constructor when the struct needs valid values from the start, or when one member is calculated from another, as area is in the Square example. For a plain bundle of values, an aggregate is often simpler.

Where the object lives also matters. A local struct lives on the stack, while new Point(3, 4) creates one on the heap, and the constructor runs in both cases. For the memory side, see c heap vs stack.

Keep exploring

Guides from other topics

All topics →

NULL vs nullptr in C++

NULL is an older macro, usually defined as 0. nullptr, added in C++11, is a typed null pointer that avoids overload surprises. Here's how to choose.

Is C++ Hard to Learn?

Is C++ hard to learn? This guide explains which parts trip up beginners, how C++ compares with C, and a practical order for learning it step by step.

Is C++ Hard 5 min read