Skip to content

Last updated:2026-10-13


comments: true keywords: C++ struct vs class,default access,default inheritance,aggregate description: "Compare C++ struct and class with compilable examples: default member access, default inheritance, and when each keyword is clearer in an API." title: C++ struct vs class: Default Access and Inheritance | C++那些事


C++ struct vs class: what changes?

In C++, both keywords define a class type. They support constructors, member functions, templates, inheritance and virtual functions. The language differences are the default access of members and the default access of base classes. Code conventions are a separate design choice.

Omitted access specifier struct class
Members public private
Base class public private

Member access: the first default

struct Open {
    int value = 1;          // public by default
};

class Closed {
    int value = 1;          // private by default
public:
    int get() const { return value; }
};

int main() {
    Open a;
    Closed b;
    // b.value = 2;         // Error: private member
    return a.value == b.get() ? 0 : 1;
}

Explicit public: or private: overrides either default. A struct can hide members; a class can expose them. For a longer comparison with C structs and name lookup, read C and C++ struct.

Inheritance access: the second default

#include <type_traits>

struct Base {};
struct PublicChild : Base {};  // equivalent to : public Base
class PrivateChild : Base {};  // equivalent to : private Base

static_assert(std::is_base_of_v<Base, PublicChild>);
static_assert(std::is_base_of_v<Base, PrivateChild>);
static_assert(std::is_convertible_v<PublicChild*, Base*>);
static_assert(!std::is_convertible_v<PrivateChild*, Base*>);

int main() {}

Compile with c++ -std=c++17 -Wall -Wextra struct_class.cpp. PrivateChild still has Base as a base; the implicit pointer conversion is inaccessible to outside code. Write : public Base or : private Base explicitly when the distinction matters to readers.

Is a struct always a plain data aggregate?

No. struct is often used for simple public data and class for types that protect invariants, but these are conventions. Aggregate status depends on properties such as constructors, access, bases and virtual functions, not on the keyword alone. Both of these are aggregates in C++17:

#include <type_traits>

struct Point { int x; int y; };
class Pair {
public:
    int first;
    int second;
};

static_assert(std::is_aggregate_v<Point>);
static_assert(std::is_aggregate_v<Pair>);

int main() {
    Point p{1, 2};
    Pair q{1, 2};
    return p.x == q.first ? 0 : 1;
}

Which should you choose?

  • Prefer struct for an intentionally public record whose members are part of the interface.
  • Prefer class when callers should interact through operations that maintain an invariant.
  • State access and inheritance explicitly in public APIs when relying on a default would obscure intent.

The choice does not change an object into a “C style struct” or grant special performance. For other interface decisions, see explicit conversions and smart pointers as parameters.

References

About the author

Personal WeChat official account:

struct vs class