25
  • When should I use a struct instead of a class? I'm currently using classes for everything from OpenGL texture wrappers to bitmap fonts.

  • Is a class that I use just like a struct (no making usage of inheritance, polymorphism, etc.) still slower than a struct?

Markus P.
  • 279
  • 1
  • 3
  • 4
  • 2
    There are a number of other very similar questions already on SO. – luke Nov 03 '10 at 19:55
  • And none of them concerns efficiency of either, do they, now? – Armen Tsirunyan Nov 03 '10 at 20:09
  • 3
    @Armen: no, they just say *they are exactly the same*. Then it is left as an exercise for the reader to make the leap of deduction that *if they are exactly the same*, then *they are probably equally efficient too*. – jalf Nov 03 '10 at 21:34
  • 1
    @jalf: Interesting form of deduction. :) If they are exactly the same, then the performance *is* exactly the same. – GManNickG Nov 03 '10 at 22:50

5 Answers5

41

Structs and classes in C++ as you may know differ solely by their default access level (and default accessibility of their bases: public for struct, private for class).

Some developers, including myself prefer to use structs for POD-types, that is, with C-style structs, with no virtual functions, bases etc. Structs should not have behavior - they are just a comglomerate of data put into one object.

But that is naturally a matter of style, and obviously neither is slower

Community
  • 1
  • 1
Armen Tsirunyan
  • 130,161
  • 59
  • 324
  • 434
  • 6
    For clarification: a "POD type" means "plain old data", and refers to a struct or class that is basically the same as a C struct in terms of performance. See http://www.fnal.gov/docs/working-groups/fpcltf/Pkg/ISOcxx/doc/POD.html for more details. And as Armen notes, you can declare POD types as either class or struct without any performance difference; it is simply style. – Kristopher Johnson Nov 03 '10 at 19:30
  • 1
    The pedant would note that `class` could also be used instead of `typename` to declare a template parameter, while `struct` cannot. But that's just a syntactical nitpick without true relevance to the question. `:)` Good answer, `+1` from me. – sbi Nov 03 '10 at 20:30
  • 2
    @sbi: The funny thing is that I did consider writing about that syntactic "difference" too, but thought it would be overly pedantic... You never know what reaction it would cause :)) – Armen Tsirunyan Nov 03 '10 at 20:54
  • 1
    @Armen: Of course, had _you_ listed this yourself, I'd have quickly pointed out that it is _totally irrelevant_ for the question at hand and had best been left out of your answer. `:)` – sbi Nov 03 '10 at 21:02
  • @sbi: I am absolutely sure you would do exactly that :))) – Armen Tsirunyan Nov 03 '10 at 21:05
  • Damn. Am I really _that_ predictable? `:(` – sbi Nov 03 '10 at 21:29
  • 1
    @sbi: when it comes to pedantry? Most C++ people are. ;) – jalf Nov 03 '10 at 21:36
15

1) There is no real difference between the 2 other than the fact that struct members are, by default, public where classes are private.

2) No its EXACTLY the same.

Edit: Bear in mind you can use virtual inheritance with structs. They are THAT identical :)

Goz
  • 61,365
  • 24
  • 124
  • 204
9

Instead of cheaping out and referring to other questions, I'll re-iterate what others have said before I add on to them.

struct and class are identical in C++, the only exception being that struct has a default access of public, and class has a default access of private. Performance and language feature support are identical.

Idiomatically, though, struct is mostly used for "dumb" classes (plain-old-data). class is used more to embody a true class.

In addition, I've also used struct for locally defined function objects, such as:

struct something
{
    something() : count(0) { }
    void operator()(int value) { std::cout << value << "-" << count++ << "\n"; }
    int count;
} doSomething;

std::vector<int> values;

...

std::foreach(values.begin(); values.end(); doSomething);
Merlyn Morgan-Graham
  • 58,163
  • 16
  • 128
  • 183
  • Love the example as it is something that I have yet to encounter in any elementary c++ class (educational =D no pun intended). – g19fanatic Nov 04 '10 at 16:12
  • @g19fanatic: They're probably waiting for lambdas. Or going "locally defined function - why would I want *that* /SticksTongueOut"? – Merlyn Morgan-Graham Nov 04 '10 at 16:18
1

I like to use classes when I need to have an explicit destructor. Because then, you should be following the rule of three, in which case you need to write a copy constructer and assignment overloader. With all of this, it seems more natural to use a class than a struct.

Tommy
  • 12,588
  • 14
  • 59
  • 110
1

as others have explained, they're the same thing except for default access levels.

the only reason why classes can be perceived to be slower is because a good style (but not the only one) is the one mentioned by ArmenTsirunyan: struct for POD types, class for full-fledged object classes. The latter ones usually include inheritance and virtual methods, hence vtables, which are slightly slower to call than straight functions.

Community
  • 1
  • 1
Javier
  • 60,510
  • 8
  • 78
  • 126