Struct vs Class: The One Difference That Actually Matters
April 23, 2026 · Luciano Muratore
Struct vs Class: The One Difference That Actually Matters
In C++, struct and class are more similar than most people expect. Both can have constructors, methods, inheritance, and private members. Once you know this, a legitimate question arises: is there any real difference between them?
The answer is yes—but it is exactly one thing.
The Only Difference
The sole technical difference between struct and class in C++ is the default member access:
- In a
class, members are private by default. - In a
struct, members are public by default.
That is it. Everything else—constructors, destructors, virtual methods, inheritance, access specifiers—works identically in both.
class MyClass {
int x; // private by default
};
struct MyStruct {
int x; // public by default
};
In MyClass, x is inaccessible from outside without an explicit public: specifier. In MyStruct, x is accessible directly.
Structs Can Have Private Members Too
The default being public does not mean structs are limited to public members. A struct can use private: explicitly, just like a class can use public:. The following is perfectly valid:
struct MyStruct {
int x; // public by default
MyStruct(int val) : x(val) {}
void show() {
cout << "x = " << x << endl;
secret();
}
private:
void secret() {
cout << "This is a private method inside a struct 🔒" << endl;
}
};
Here, x and show are public. secret is private—accessible only from within the struct. The public method show acts as the interface, and secret is an implementation detail hidden from the outside. This is encapsulation, just as you would find in any class.
Putting It Together
int main() {
MyStruct s(10);
s.show(); // works — show() is public
MyClass obj;
obj.x = 5; // Error: 'x' is private in 'MyClass'
return 0;
}
s.show() works because show is public and internally calls secret, which is private but accessible within the struct itself. obj.x = 5 fails to compile because MyClass members are private by default, and x was never explicitly made public.
When to Use Which
In practice, the choice between struct and class is often a matter of convention and intent:
- Use
structfor simple data aggregates where everything is naturally public—plain data holders, POD types, configuration bundles. - Use
classwhen you want the default to reinforce encapsulation—when the type has invariants to protect and its internals should not be touched directly.
Both are capable of the same things. The default access is just a hint to the reader about how the type is meant to be used.
Summary
- In C++,
structandclassdiffer in exactly one way: default member access (publicvsprivate). - A
structcan have private members, constructors, methods, and everything aclasscan. - The
private:specifier works instructjust aspublic:works inclass. - The choice between them is largely conventional: structs signal open data, classes signal encapsulated behavior.
- Final Insight: The difference between
structandclassis not about capability—it is about intent. One says “here is some data,” the other says “here is an object with an interface.” The compiler does not care. The reader does.