C++ - Struct VS. Class

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++ are:

  • struct members and base classes/structs are public by default.
  • class members and base classes/structs 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:

  • use struct for plain-old-data structures without any class-like features;
  • use class when you make use of features such as private or protected 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.

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.

  1. 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.

What's the difference between struct and class in .NET?

In .NET, there are two categories of types, reference types and value types.

Structs are value types and classes are reference types.

The general difference is that a reference type lives on the heap, and a value type lives inline, that is, wherever it is your variable or field is defined.

A variable containing a value type contains the entire value type value. For a struct, that means that the variable contains the entire struct, with all its fields.

A variable containing a reference type contains a pointer, or a reference to somewhere else in memory where the actual value resides.

This has one benefit, to begin with:

  • value types always contains a value
  • reference types can contain a null-reference, meaning that they don't refer to anything at all at the moment

Internally, reference types are implemented as pointers, and knowing that, and knowing how variable assignment works, there are other behavioral patterns:

  • copying the contents of a value type variable into another variable, copies the entire contents into the new variable, making the two distinct. In other words, after the copy, changes to one won't affect the other
  • copying the contents of a reference type variable into another variable, copies the reference, which means you now have two references to the same somewhere else storage of the actual data. In other words, after the copy, changing the data in one reference will appear to affect the other as well, but only because you're really just looking at the same data both places

When you declare variables or fields, here's how the two types differ:

  • variable: value type lives on the stack, reference type lives on the stack as a pointer to somewhere in heap memory where the actual memory lives (though note Eric Lipperts article series: The Stack Is An Implementation Detail.)
  • class/struct-field: value type lives completely inside the type, reference type lives inside the type as a pointer to somewhere in heap memory where the actual memory lives.

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 are public by default,
while in class, they default to private. Note: you should make
your base classes explicitly public, private, or protected,
rather than relying on the defaults.

struct and class are otherwise functionally equivalent.

OK, enough of that squeaky clean techno talk. Emotionally, most
developers make a strong distinction between a class and a struct.
A struct simply feels like an open pile of bits with very little
in the way of encapsulation or functionality. A class 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 the struct keyword if you have a class that has very few methods
and has public data (such things do exist in well designed
systems!), but otherwise you should probably use the class 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.

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.

How is the memory layout of a class vs. a struct

Is there a guarantee e.g. that the public variables will be first in
memory then the private variable?

No, such a guarantee is not made - C++11 standard, [class.mem]/14:

Nonstatic data members of a (non-union) class with the same access
control
(Clause 11) are allocated so that later members have higher
addresses within a class object. The order of allocation of non-static
data members with different access control is unspecified
(11).

So

struct A
{
int i, j;
std::string str;

private:

float f;

protected:

double d;
};

It is only guaranteed that, for a given object of type A,

  • i has a smaller address than j and
  • j has a smaller address than str

Note that the class-keys struct and class have no difference regarding layout whatsoever: Their only difference are access-rights which only exist at compile-time.


It only says the order, but not that the first variable actually start
at the "first address"? Lets assume a class without inheritance.

Yes, but only for standard-layout classes. There is a row of requirements a class must satisfy to be a standard-layout class, one of them being that all members have the same access-control.

Quoting C++14 (the same applies for C++11, but the wording is more indirect), [class.mem]/19:

If a standard-layout class object has any non-static data members, its
address is the same as the address of its first non-static data
member. Otherwise, its address is the same as the address of its first
base classsubobject (if any). [ Note: There might therefore be
unnamed padding within a standard-layout struct object, but not at its beginning, as necessary to achieve appropriate alignment. — end note ]

[class]/7:

A standard-layout class is a class that:

  • has no non-static data members of type non-standard-layout class (or array of such types) or reference,
  • has no virtual functions (10.3) and no virtual base classes (10.1),
  • has the same access control (Clause 11) for all non-static data members,
  • has no non-standard-layout base classes,
  • either has no non-static data members in the most derived class and at most one base class with non-static data members, or has no base
    classes with non-static data members, and
  • has no base classes of the same type as the first non-static data member. 110

110) This ensures that two subobjects that have the same class type and that belong to the same most derived object are not
allocated at the same address (5.10).



Related Topics



Leave a reply



Submit