When Should You Use a Class VS a Struct in C++

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

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

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



Leave a reply



Submit