When should you use a class vs a struct in C++?
The differences between a class
and a struct
in C++ is:
-
struct
members and base classes/structs arepublic
by default. -
class
members and base classes/struts areprivate
by default.
Both classes and structs can have a mixture of public
, protected
and private
members, can use inheritance and can have member functions.
I would recommend you:
- use
struct
for plain-old-data structures without any class-like features; - use
class
when you make use of features such asprivate
orprotected
members, non-default constructors and operators, etc.
As everyone else notes there are really only two actual language differences:
-
struct
defaults to public access andclass
defaults to private access. - When inheriting,
struct
defaults topublic
inheritance andclass
defaults toprivate
inheritance. (Ironically, as with so many things in C++, the default is backwards:public
inheritance is by far the more common choice, but people rarely declarestruct
s just to save on typing the "public
" keyword.
But the real difference in practice is between a class
/struct
that declares a constructor/destructor and one that doesn't. There are certain guarantees to a "plain-old-data" POD type, that no longer apply once you take over the class's construction. To keep this distinction clear, many people deliberately only use struct
s for POD types, and, if they are going to add any methods at all, use class
es. The difference between the two fragments below is otherwise meaningless:
class X
{
public:
// ...
};
struct X
{
// ...
};
(Incidentally, here's a thread with some good explanations about what "POD type" actually means: What are POD types in C++?)
There are lots of misconceptions in the existing answers.
Both class
and struct
declare a class.
Yes, you may have to rearrange your access modifying keywords inside the class definition, depending on which keyword you used to declare the class.
But, beyond syntax, the only reason to choose one over the other is convention/style/preference.
Some people like to stick with the struct
keyword for classes without member functions, because the resulting definition "looks like" a simple structure from C.
Similarly, some people like to use the class
keyword for classes with member functions and private
data, because it says "class" on it and therefore looks like examples from their favourite book on object-oriented programming.
The reality is that this completely up to you and your team, and it'll make literally no difference whatsoever to your program.
The following two classes are absolutely equivalent in every way except their name:
struct Foo
{
int x;
};
class Bar
{
public:
int x;
};
You can even switch keywords when redeclaring:
class Foo;
struct Bar;
(although this breaks Visual Studio builds due to non-conformance, so that compiler will emit a warning when you do this.)
and the following expressions both evaluate to true:
std::is_class<Foo>::value
std::is_class<Bar>::value
Do note, though, that you can't switch the keywords when redefining; this is only because (per the one-definition rule) duplicate class definitions across translation units must "consist of the same sequence of tokens". This means you can't even exchange const int member;
with int const member;
, and has nothing to do with the semantics of class
or struct
.