C/C++ Struct vs Class
In C++, structs and classes are pretty much the same; the only difference is that where access modifiers (for member variables, methods, and base classes) in classes default to private, access modifiers in structs default to public.
However, in C, a struct is just an aggregate collection of (public) data, and has no other class-like features: no methods, no constructor, no base classes, etc. Although C++ inherited the keyword, it extended the semantics. (This, however, is why things default to public in structs—a struct written like a C struct behaves like one.)
While it's possible to fake some OOP in C—for instance, defining functions which all take a pointer to a struct as their first parameter, or occasionally coercing structs with the same first few fields to be "sub/superclasses"—it's always sort of bolted on, and isn't really part of the language.
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.
Why do we need a `class` in C++, when a `struct` can be used to achieve the same?
You don't need classes, the language just gives you another option to choose from. Technically, you're right, you can achieve anything a class
can do with a struct
.
Besides the default access level, there's also the meaning most programmers associate with the two - struct
generally means a light-weight, typically POD
, data-type with little to no functionality. A class
is usually associated with something bigger.
When should I use a struct instead of a class?
MSDN has the answer:
Choosing Between Classes and Structures.
Basically, that page gives you a 4-item checklist and says to use a class unless your type meets all of the criteria.
Do not define a structure unless the
type has all of the following
characteristics:
- It logically represents a single value, similar to primitive types
(integer, double, and so on).- It has an instance size smaller than 16 bytes.
- It is immutable.
- It will not have to be boxed frequently.
When to use struct over class in c++ [duplicate]
The only difference between the two is that by default struct
are public while class
members are private.
My rule is to use struct
when I mean a clump of related data without any special semantics on reading/writing them. Use class
when I intend to wrap the data in richer semantics (read methods) that are more meaningful, provide any needed protection, or implement something more abstract.
What are the differences between struct and class in C++?
You forget the tricky 2nd difference between classes and structs.
Quoth the standard (§11.2.2 in C++98 through C++11):
In absence of an access-specifier
for a base class, public is assumed
when the derived class is declared
struct and private is assumed when the class is declared class.
And just for completeness' sake, the more widely known difference between class and struct is defined in (11.2):
Member of a class defined with the
keyword class are private by
default. Members of a class defined
with the keywords struct or union
are public by default.
Additional difference: the keyword class
can be used to declare template parameters, while the struct
keyword cannot be so used.
Structs vs classes in C++ [duplicate]
Technically, the only difference between the two is that structs are public: by default and classes are private:
Other than that, there is no technical difference.
struct vs class then becomes a purely expressive nuance of the language.
Usually, you avoid putting complicated methods in a struct, and most of the time structs data members will stay public. In a class you want to enforce strong encapsulation.
struct = data is public, with very simple helper methods
class = strongly encapsulated, data is modified / accessed only through methods
Related Topics
Find Middle Elements from an Array
How to Measure Memory Usage from Inside a C++ Program
Two Dimensional Array With Random Numbers Doesn't Change
What Are the Rules About Using an Underscore in a C++ Identifier
How to Set, Clear, and Toggle a Single Bit
Start Thread With Member Function
When a Function Has a Specific-Size Array Parameter, Why Is It Replaced With a Pointer
What Does "Dereferencing" a Pointer Mean
How to Automatically Generate a Stacktrace When My Program Crashes
What Do the Following Phrases Mean in C++: Zero-, Default- and Value-Initialization
What Is Ownership of Resources or Pointers
How Does Delete[] "Know" the Size of the Operand Array
Unnamed/Anonymous Namespaces Vs. Static Functions
Utf8 To/From Wide Char Conversion in Stl
Timer Function to Provide Time in Nano Seconds Using C++
How Come a Non-Const Reference Cannot Bind to a Temporary Object