29

When should someone use structs instead of classes or vice versa in C++? I find myself using structs when a full-blown class managing some information seems like overkill but want to indicate the information being contained are all related. I was wondering what are some good guidelines to be able to tell when one is more appropriate than the other?

Edit: Found these links while reading the material Stack Overflow indicated was related after the question was submitted:

When should you use a class vs a struct in C++?
What are the differences between struct and class in C++?

Community
  • 1
  • 1
dlanod
  • 8,664
  • 8
  • 54
  • 96

10 Answers10

20

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

Coincoin
  • 27,880
  • 7
  • 55
  • 76
6

I use structs for simple containers of types that provide no constructors or operators.

Classes for everything else.

Andrew Grant
  • 58,260
  • 22
  • 130
  • 143
4

Use a struct when you simply need a "bucket of stuff" that doesn't have logical invariants that you need to keep. Use a class for anything else.

See also what the C++ FAQ says on the subject.

dgellow
  • 692
  • 1
  • 11
  • 18
Brian Neal
  • 31,821
  • 7
  • 55
  • 59
2

Use a class if you have methods, a struct if not.

A class should hide all its internals only exposing methods or properties. A struct tends to expose all its internals and has no accessor methods.

Where only one bit of code is accessing some (related) data, a struct may be perfectly reasonable. Where multiple bits of code need to modify the data or if it's anything slightly complicated, a class would be a better bet.

Mat
  • 82,161
  • 34
  • 89
  • 109
2

The difference between Classes and Structs are that structs are groups of variables and classes represent objects. Objects have attributes AND methods and be part of a hierarchy.

If you're using C++ to take advantage of the OO capabilities it's best to use classes / objects which are more natural.

Philluminati
  • 2,649
  • 2
  • 25
  • 32
  • 1
    best answer I've ever seen "structs are groups of variables and classes represent objects" – Joy Apr 30 '12 at 15:49
1

I always use class, even for just containers, for consistency. Its purely a choice of style since the difference between the two is negligible.

jheriko
  • 3,043
  • 1
  • 21
  • 28
1

If you need to control access to the data, you should use classes. If you don't care who is accessing what, and what they're storing in there, then a struct is probably more appropriate.

Also, a class is more appropriate if you need to do any checks on the integrity of the data itself.

Nik Reiman
  • 39,067
  • 29
  • 104
  • 160
  • 1
    I found two downvotes on this answer, which looks perfectly good to me. What gives? – David Thornley Feb 12 '09 at 22:25
  • Beats me. Did I get something wrong here? If I'm totally wrong about this, then I'd really like to know how... – Nik Reiman Feb 12 '09 at 23:10
  • thx. I don't really care if it's the top rated answer; I just want to know why it was wrong... apparently, 2 people thought it was kind of wrong. Ah, well. – Nik Reiman Feb 13 '09 at 18:53
0

See existing questions:

What are the differences between struct and class in C++

When should you use a class vs a struct in C++?

Community
  • 1
  • 1
crashmstr
  • 28,043
  • 9
  • 61
  • 79
0

Personally, I use structs when all I need is a container for data (no member functions). Otherwise, I use classes.

The only time I make an exception to that rule is if I need a simple functor: e.g.

struct compare { bool operator() { ... } };
sort(v.begin(), v.end(), compare());

The need for a public: label would just clutter up the code unnecessarity.

Ferruccio
  • 98,941
  • 38
  • 226
  • 299
0

structs in C++ are classes with a default access method of public, so technically other than that default there is no difference and you can use both equivalently.

Yet there are some expectations and natural tendencies, in part because structs in C++ come from C.

My approach: If it has any private data, a constructor/destructor, or any complex member functions (which do more than just conversion upon set/get, etc.), use class.