skip to content

In C++, what actually differs between a type declared with the keyword struct and one declared with class?

level: juniorimportance: must knowfreq 72%

answer

  1. only defaults change
  2. default member access
  3. default base-class access
  4. an explicit specifier overrides both

basics

~20 s

Only two defaults differ: struct members and base classes are public by default, class members and base classes are private. Otherwise the keywords are interchangeable, with the same constructors, virtual functions, templates and access specifiers.

solid answer

~50 s

The class-key changes two defaults and nothing else. Members declared before any access specifier are `public` in a `struct` and `private` in a `class`. A base class listed without an access specifier is inherited `public` when the derived type is a `struct` and `private` when it is a `class`, so `struct D : B` and `class D : B` differ. Both can have constructors, destructors, virtual functions, member templates, friends and every access specifier, and an explicit `public:` or `private:` overrides the default either way. Choosing between them is a convention: many teams write `struct` for a bundle of public data with no invariant and `class` for a type that protects an invariant behind private members. One wrinkle: declaring `struct Cache;` and defining `class Cache` is valid, but Clang's opt-in `-Wmismatched-tags` warns because the Microsoft C++ ABI can mangle the two differently.

code

cpp · 11 lines
cpp
struct S { int x = 1; };   // members public by default
class C { int x = 2; };    // members private by default
struct D1 : C {};          // C is a public base by default
class D2 : S {};           // S is a private base by default
int main() {
    S s; C c; D2 d2;
    int a = s.x;     // OK
    int b = c.x;     // error
    int e = d2.x;    // error: S is a private base of D2
    return a + b + e;
}

go deeper

for a junior

Recall the two defaults: struct members and bases are public, class members and bases are private. Say clearly that nothing else differs.

for a middle

Show the base-class default with struct D : B versus class D : B, and explain why an explicit access specifier makes the keyword irrelevant.

for a senior

Connect the keyword to conventions: struct for passive records without invariants, class for types that guard invariants, and keep it consistent across declarations because of the mismatched-tags issue.

for a principal

Treat the keyword as a readability signal a style guide can standardise, and weigh enforcing it in review against the near-zero technical difference it carries.

## Two defaults, nothing else In C++ the keywords `struct` and `class` are both **class-keys**: each introduces a class type, and the standard treats the resulting types identically except for two defaults. The C++ standard states the first directly: members of a class defined with the keyword `class` are private by default, and members of a class defined with `struct` or `union` are public by default. | What the default controls | `struct` | `class` | |---|---|---| | Access of members declared before any access specifier | `public` | `private` | | Access of a base class listed without an access specifier | `public` | `private` | The second default belongs to the **derived** type's keyword: in `struct D6 : B` the base `B` is public, and in `class D3 : B` it is private, whatever keyword `B` itself was declared with. How public, protected and private inheritance differ in detail is material for the lesson on inheritance; here it is enough that the default flips with the keyword. ## What does not differ Everything else is shared, which surprises candidates who learned C first: - A `struct` can have **constructors, a destructor and overloaded operators**. - A `struct` can have **virtual functions** and be used polymorphically. - A `struct` can declare **`private:` and `protected:` sections**, and a `class` can declare `public:` ones. - Both can be **templates**, contain **nested types**, declare **friends** and have **static members**. - Object layout, `sizeof` and the special members the compiler generates do not depend on the keyword. - The `class` in `template <class T>` is a different use of the keyword, interchangeable with `typename` in a template parameter list and unrelated to this choice. The access specifiers themselves mean the same thing in both: **`public`** members are usable by any code, **`protected`** members by the class, its friends and its derived classes, and **`private`** members only by the class and its friends. ## Seeing both defaults in the compiler The snippet in the code example declares `struct S { int x = 1; };`, `class C { int x = 2; };` and `class D2 : S {};`. Reading `c.x` fails, and Clang explains which default bit: ```plaintext access.cpp:8:15: error: 'x' is a private member of 'C' access.cpp:2:15: note: implicitly declared private here access.cpp:9:16: error: 'x' is a private member of 'S' access.cpp:4:12: note: constrained by implicitly private inheritance here ``` The second error is the base-class default: `x` is public inside `S`, but `D2` was declared with `class`, so `S` became a private base and `d2.x` is inaccessible from `main`. ## Aggregates depend on access, not on the keyword A common belief is that only a `struct` can be brace-initialised member by member. The rule for an **aggregate** is about contents: no user-declared or inherited constructors, no private or protected direct non-static data members, no private or protected direct base classes and no virtual functions or virtual bases. A `class` whose members are all under `public:` and that declares no constructor is an aggregate; a `struct` with one private data member is not. ## The conventions teams adopt Because the language barely cares, the keyword becomes a signal to readers: 1. Use `struct` for a **passive record** whose members can take any combination of values: a point, a configuration bundle, a message header. 2. Use `class` once the type has an **invariant** that its member functions maintain, so the data goes behind `private:`. 3. Keep the keyword consistent across every declaration of the same type. The third point has a practical edge. Forward-declaring `struct Cache;` and later defining `class Cache` is valid C++, yet Clang warns about it when `-Wmismatched-tags` is enabled (it is off by default): ```plaintext tags.cpp:2:1: warning: 'Cache' defined as a class here but previously declared as a struct; this is valid, but may result in linker errors under the Microsoft C++ ABI [-Wmismatched-tags] ``` The warning exists because the Microsoft C++ ABI records the class-key in mangled names, so a header that says `struct` and a source file that says `class` can produce symbols that fail to match at link time on that ABI. ## What to say in the interview State the two defaults, give the inheritance example, and add that the keyword changes no capability. Mentioning the aggregate rule and the mismatched-tags warning shows you have met the edges of the rule in real code, not only in a textbook.

  • Does choosing struct or class change whether a C++ type is an aggregate?
    Not by itself. An aggregate needs no user-declared or inherited constructors, no private or protected direct non-static data members, no private or protected direct bases and no virtual functions or virtual bases. A `class` whose members are all `public` and that declares no constructor qualifies; a `struct` with one `private` data member does not. The keyword only matters because it changes the default access of members you did not label.
  • Can a C++ class declared with struct have virtual functions and be used through a base pointer?
    Yes. Virtual functions, virtual destructors, abstract bases and dynamic dispatch work identically for both keywords. Many interface types in real codebases are written as `struct IRenderer { virtual ~IRenderer() = default; virtual void draw() = 0; };` precisely because every member is meant to be public, so the `struct` default saves a `public:` line.

  • A struct in C++ cannot have constructors, member functions or virtual functions.
  • Structs are allocated on the stack and classes on the heap.
  • Members of a struct are always public and cannot be made private.
  • struct D : B makes B a private base, the same as class.
  • The keyword changes the object's memory layout or its size.