This isn't only applicable to C++, but to any language that provides both structs and classes.
–
Jason BuntingSep 10 '08 at 16:31

2

I still disagree - I approach this question semantically. Maybe there are some technical differences, but semantically, they are not. Structs are really useful for creating value types, classes are not.
–
Jason BuntingSep 10 '08 at 17:22

1

I find that I rarely use struct in C++, except when I need auto aggregate initialization of Plain Old Data, I don't think that can be done with data structures defined with an instance of a class.
–
Roger NelsonSep 17 '08 at 5:06

1

Struct - For POD (plain old data) and all member accessibility is public. Class - When you need better encapsulation and need member functions to work with the class's state.
–
AppuJul 28 '09 at 3:51

1

@Dave - that convention is the most important reason to do this. If it is followed consistently, that single difference immediately provides a lot of information to readers.
–
TomOct 27 '09 at 2:32

19 Answers
19

The only difference between a class and a struct in C++ is that structs have default public members and bases and classes have default private members and bases. Both classes and structs can have a mixture of public and private members, can use inheritance, and can have member functions.

I would recommend using structs as plain-old-data structures without any class-like features, and using classes as aggregate data structures with private data and member functions.

A struct with no modifiers or methods is called a POD struct, which exists as a backwards compatible interface with C libraries as it is (supposedly) guaranteed to be laid out as though it were a C struct. Apart from this one exception though, the only difference is as stated.
–
workmad3Sep 18 '08 at 14:22

9

@workmad3: The name is misleading, but 9/4 (C++03) says: "A POD-struct is an aggregate class that has no non-static data members of type non-POD-struct, non-POD-union (or array of such types) or reference, and has no user-defined copy assignment operator and no user-defined destructor." There is no restriction on using the "struct" class-key and no restriction on using "public" (see 8.5.1/1 for aggregate requirements). This is not a difference between "struct" and "class".
–
Roger PateSep 22 '10 at 20:31

3

Your use of "aggregate" could be misunderstood, given the standard's definition. :)
–
Roger PateSep 22 '10 at 20:32

3

@wordmad3: "Apart from this one exception though, the only difference is as stated." Why does this total nonsense have 35 upvotes? A class with the same properties is POD too. Aside from the defaults imbued depending upon the keyword used at declaration, there is absolutely no semantic difference between "a struct" and "a class"; indeed, they are in fact called the same thing! You are defining a user-defined type nothing more nothing less.
–
Lightness Races in OrbitFeb 7 at 4:50

As everyone else notes there are really only two actual language differences:

struct defaults to public access and class defaults to private access.

When inheriting from them struct defaults to public inheritance and class defaults to private inheritance. (Ironically, as with so many things in C++, the default is backwards: public inheritance is by far the more common choice, but people rarely declare structs just to save on typing the "public" keyword.

But the real difference in practice is between a class/struct that declares a constructor/destructor and one that doesn't. There are certain guarantees to a "plain-old-data" POD type, that no longer apply once you take over the class's construction. To keep this distinction clear, many people deliberately only use structs for POD types, and, if they are going to add any methods at all, use classes. The difference between the two fragments below is otherwise meaningless:

Your note about inheritance is false -- the default is based on whether the derived class is a struct or a class (the type of the base class makes no difference): see [class.access.base]/2: In the absence of an access-specifier for a base class, public is assumed when the derived class is defined with the class-key struct and private is assumed when the class is defined with the class-key class.
–
MankarseNov 7 '12 at 9:59

Whether you use struct or class has no bearing on whether your object is POD or whether you should define a copy constructor / destructor. Member functions also have no bearing on something being POD. As I re-read what you wrote, I see you aren't suggesting otherwise, but the current wording is confusing
–
David StoneNov 23 '14 at 0:50

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.

One place where a struct has been helpful for me is when I have a system that's receiving fixed format messages (over say, a serial port) from another system. You can cast the stream of bytes into a struct that defines your fields, and then easily access the fields.

Structs (PODs, more generally) are handy when you're providing a C-compatible interface with a C++ implementation, since they're portable across language borders and linker formats.

If that's not a concern to you, then I suppose the use of the "struct" instead of "class" is a good communicator of intent (as @ZeroSignal said above). Structs also have more predictable copying semantics, so they're useful for data you intend to write to external media or send across the wire.

Structs are also handy for various metaprogramming tasks, like traits templates that just expose a bunch of dependent typedefs:

For C++, there really isn't much of a difference between structs and classes. The main functional difference is that members of a struct are public by default, while they are private by default in classes. Otherwise, as far as the language is concerned, they are equivalent.

That said, I tend to use structs in C++ like I do in C#, similar to what Brian has said. Structs are simple data containers, while classes are used for objects that need to act on the data in addition to just holding on to it.

they're the same thing with different defaults (private by default for class, and public by default for struct), so in theory they're totally interchangeable.

so, if I just want to package some info to move around, I use a struct, even if i put a few methods there (but not many). If it's a mostly-opaque thing, where the main use would be via methods, and not directly to the data members, i use a full class.

As every one says, the only real difference is the default access. But I particularly use struct when I don't want any sort of encapsulation with a simple data class, even if I implement some helper methods. For instance, when I need something like this:

You can use "struct" in C++ if you are writing a library whose internals are C++ but the API can be called by either C or C++ code. You simply make a single header that contains structs and global API functions that you expose to both C and C++ code as this:

Then you can write a function bar() in a C++ file using C++ code and make it callable from C and the two worlds can share data through the declared struct's. There are other caveats of course when mixing C and C++ but this is a simplified example.

To answer my own question (shamelessly), As already mentioned, access privileges are the only difference between them in C++.

I tend to use a struct for data-storage only. I'll allow it to get a few helper functions if it makes working with the data easier. However as soon as the data requires flow control (i.e. getters/setters that maintain or protect an internal state) or starts acquring any major functionality (basically more object-like), it will get 'upgraded' to a class to better communicate intent.

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

1

This does not provide an answer to the question. To critique or request clarification from an author, leave a comment below their post.
–
Hugo S FerreiraAug 29 '14 at 19:43

Access functions aren't the only difference between structs and classes. A struct is better for POD (Plain Old Data) types, since it is easier to manage for the compiler and the programmer. For beginners who don't use object-oriented programming, using a struct is fine.

I can't ever imagine a scenario where you would use a struct when you want private members, unless you're willfully trying to be confusing.

It seems that using structs is more of a syntactic indication of how the data will be used, but I'd rather just make a class and try to make that explicit in the name of the class, or through comments.

According to me, a "syntactic indication of how data will be used" is a perfectly good reason to use a struct, especially if the alternative is to use a comment or a name in the classname.
–
Viktor SehrJun 26 '09 at 11:41

Wouldn't declaring a struct already be pretty explicit that the members of the class will be, by default, public?
–
wrongusernameSep 15 '11 at 4:57