views:

542

answers:

10

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:

http://stackoverflow.com/questions/54585/when-should-you-use-a-class-vs-a-struct-in-c http://stackoverflow.com/questions/92859/what-are-the-differences-between-struct-and-class-in-c http://stackoverflow.com/questions/85553/when-should-i-use-a-struct-instead-of-a-class

+1  A: 

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
+9  A: 

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
However, the OP was asking for guidelines, which depend on the expressive nuance.
David Thornley
+4  A: 

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

Classes for everything else.

Andrew Grant
A: 

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

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
That is pretty much google c++ guideline. Not that I follow/like their guidelines in many other aspects.
David Rodríguez - dribeas
A: 

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.

A: 

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
+2  A: 

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.

Brian Neal
+1  A: 

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
I found two downvotes on this answer, which looks perfectly good to me. What gives?
David Thornley
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
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