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.
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.
Performance C struct vs C++ struct/class
Which is faster when passing these structs to functions/methods?
A member function should be exactly as fast to call as a non-member taking a pointer as an argument; since that's exactly what a (non-virtual, non-static) member function is.
The first non-member function is probably slightly faster to call than the first member, since it doesn't take a hidden this
parameter. However,it doesn't access the object it's called on, so it could be static or a non-member; in which case it will be exactly as fast as the non-member.
The second takes its hidden parameter as a pointer, so it might be slower or faster than the non-member function taking a value, depending on exactly what it's doing with it.
Since the C structs are static, only one instance reside in memory, but what happens with the C++ structs?
C structs aren't static. You can create and destroy them just like any other object - as your example does when it creates a local variable, then returns a copy of it. C++ classes are just the same in that regard.
Do its methods (func3() and func4()) occupy redundant memory for every instance
No, member functions don't occupy any memory in the class instance. Like non-member functions, the code exists in just one place; the only real difference is that member functions are passed an extra argument.
If the class has virtual functions, then that (typically) adds a single pointer, the vptr, to each object, along with a single static table of function pointers and other runtime type information (the vtable) for the class.
while passing the C++ struct, only the instance variables, a and b, are passed?
Indeed. This is a standard layout class, which means that the only things it contains are its data members, just like a C struct.
which function call to these functions is faster (if there is any difference)?
They should be the same; both pass a trivially copyable object containing the same data members by value.
Performances of Structs vs Classes
On runtime level there is no difference between structs and classes in C++ at all.
So it doesn't make any performance difference whether you use struct A
or class A
in your code.
Other thing, is using some features -- like, constructors, destructors and virtual functions, -- could have some performance penalties (but if you use them you probably need them anyway). But you can with equal success use them both inside your class or struct.
In this document you can read about other performance-related subtleties of C++.
Difference between a struct and a class
In C++, the only difference between a struct and a class is that struct members are public by default, and class members are private by default.
However, as a matter of style, it's best to use the struct
keyword for something that could reasonably be a struct in C (more or less POD types), and the class
keyword if it uses C++-specific features such as inheritance and member functions.
C does not have classes.
C structs cannot use C++-specific features.
EDIT:
The C++ FAQ Lite, question 7.9, has this to say:
The members and base classes of a
struct
arepublic
by default,
while inclass
, they default toprivate
. Note: you should make
your base classes explicitlypublic
,private
, orprotected
,
rather than relying on the defaults.
struct
andclass
are otherwise functionally equivalent.OK, enough of that squeaky clean techno talk. Emotionally, most
developers make a strong distinction between aclass
and astruct
.
Astruct
simply feels like an open pile of bits with very little
in the way of encapsulation or functionality. Aclass
feels like a
living and responsible member of society with intelligent services, a
strong encapsulation barrier, and a well defined interface. Since
that's the connotation most people already have, you should probably
use thestruct
keyword if you have a class that has very few methods
and haspublic
data (such things do exist in well designed
systems!), but otherwise you should probably use theclass
keyword.
And quoting Stroustrup's "The C++ Programming Language", 4th edition, section 16.2.4:
These two definitions of S are interchangeable, though it is
usually wise to stick to one style. Which style you use depends on
circumstances and taste. I tend to use struct for classes that I
think of as "just simple data structures." If I think of a class as "a
proper type with an invariant," I use class. Constructors and
access functions can be quite useful even for *struct*s, but as a
shorthand rather than guarantors of invariants.
Why do both struct and class exist in C++?
Why do both struct and class exist in C++?
A reason for existence of struct
is for compatibility with C.
Why then, did "C with Classes" introduce the new keyword class
when you could use struct
for the same thing, you may ask. See this SO answer for plausible speculation. In short, it's probably because there was desire for emphasis on OOP in which class is a widely used term. Only Stroustrup may know for certain.
Confusingly, the keywords themselves do not necessarily correspond to the language used in the standard
What needs to be understood, is that the concept of a class is not one and the same with the keyword class
.
There are three keywords for declaring classes. These keywords known as class-keys are class
, struct
and union
. The non-union classes that are declared with either class
or struct
are exactly the same thing, except for †. Union classes are different from non-union classes.
However, struct explicitly cannot be used in a template declaration to introduce type template parameters
C++ re-uses keywords for different purposes in different contexts. class
keyword in a class declaration context, is not entirely the same as class
keyword in a template argument definition. One keyword being equivalent to another in one context does not make it equivalent in all contexts. The reason for reusing keywords in different but similar contexts (static
is another example), is to avoid introducing new keywords, which introduces more holes with compatibility with C (or earlier C++ standard) that does not have the new keywords.
The reason why class
keyword was reused in the context of template type arguments was probably because classes are types, and therefore typically used as type parameters. There is also a typename
keyword, which was added later and is (almost) interchangeable with class
in template type argument declaration, but also used elsewhere (dependent type names) where class
is not used. See this answer for a link and a summary about why a separate keyword was added to that context.
Why struct
is not used in the context as an equivalent, you may ask. Well, that's another question to Stroustrup or the committee. It's an opposite choice than what committee did when enum class
/enum struct
was introduced.
I'm unable to see any significant difference between struct and class
Good. There isn't any except for †
This seems rather redundant and confusing while introducing a glaring inconsistency.
I see no inconsistency in the quote from the standard. I see redundancy and I suspect that the redundancy exists to make it extra clear that a class declared with the keyword struct
is still a class.
- Are there any technical differences that I have missed that significantly distinguish struct and class?
I've already answered, but to be clear, there is no difference between classes declared with struct
and class
keywords, beyond †.
† the difference with the default access specifier (as you already know, and also described here), which is their only difference.
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.
Related Topics
How to Forward Declare an Inner Class
Constants and Compiler Optimization in C++
How Do Promotion Rules Work When the Signedness on Either Side of a Binary Operator Differ
Std::String to Float or Double
Catch Exception by Pointer in C++
How Does Ifstream'S Eof() Work
Checking Cin Input Stream Produces an Integer
How to Check Whether Operator== Exists
C++ Invoke Explicit Template Constructor
How to Match Multiple Results Using Std::Regex
How to Change Mode from C++98 Mode in Dev-C++ to a Mode That Supports C++0X (Range Based For)
What Happens in a Double Delete
How to Take the Address of a Function Defined in Standard Library
How to Efficiently Perform Double/Int64 Conversions With Sse/Avx
Is There Any Real Risk to Deriving from the C++ Stl Containers