In what scenarios is it better to use a struct
vs a class
in C++?
Class instances each have an identity and are passed by reference, while structs are handled and mutated as values. Basically, if we want all of the changes that are made to a given object to be applied the same instance, then we should use a class — otherwise a struct will most likely be a more appropriate choice.
You can use it to store variables in different types. The struct type is comparable to classes in object-oriented programming. Sometimes you may need to assign values to objects with the same properties. Instead of creating multiple variables for these objects in your C program, you can define them in a struct.
Using a struct we can achieve all the functionality of a class : constructors (that can be modified/overloaded), destructors (that can be modified/overloaded), operator overloading, instance methods, static methods, public / private / protected fields/methods.
a struct is more like a data structure that is used to represent data. class, on the other hand, is more of a functionality inclined construct. It mimics the way things are and work.
The differences between a class
and a struct
in C++ is:
struct
members and base classes/structs are public
by default.class
members and base classes/struts are private
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:
struct
for plain-old-data structures without any class-like features;class
when you make use of features such as private
or protected
members, non-default constructors and operators, etc.As everyone else notes there are really only two actual language differences:
struct
defaults to public access and class
defaults to private access.struct
defaults to public
inheritance and class
defaults to private
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 declare struct
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
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With