7

Possible Duplicate:
What are the differences between struct and class in C++

I'm just trying to figure out if using 'C structs' in C++ are essentially useless or not. Do you gain anything by using them (opposed to simply creating another class)?

In C the point of structs is obvious, simply contiguous allocations of grouped data and a nice way to access said data, in C++ I feel the role becomes a little more vague.

Seeing as that you can have functions which are members of structs, instance variables, and visibility labels, the only real difference that I see between structs and classes in C++ is that struct members default to public while class members default to private. The way I see them, they can actually both be implemented with the same exact underlying system.

So am I missing something here as to the purpose of structs in C++? Or have they kind of lost their purpose, as I feel they have in C++?

Community
  • 1
  • 1
Syndacate
  • 637
  • 1
  • 7
  • 15
  • Well... it was obviously inherited from C. – Mysticial Oct 27 '12 at 20:19
  • Also http://stackoverflow.com/questions/5432681/performances-of-structs-vs-classes – Lee Taylor Oct 27 '12 at 20:21
  • 3
    Read this Part of an [Interview with Bjarne Stroustrup](http://www.artima.com/intv/goldilocks3.html) – halex Oct 27 '12 at 20:23
  • Yes, I know it was inherited from C. That wasn't the aim of my question - at all. I was wondering if you gained anything from using structs in C++, when they were 'messed up' (at least in my opinion) opposed to how they were in C. Structs in C aren't the same as structs in C++, despite the backwards compatibility, and given that they have the same exact aspects as classes, I was wondering if you gained anything by using them. I really wish structs in C++ would remain as they were in C, simply contiguous allocations of data, no functions, no visibility, no anything - just variables. – Syndacate Oct 29 '12 at 20:15

6 Answers6

4

Classes and structures in C++ are almost the same, the only things that separates them is that the default visibility of a class is private while in a struct it's public.

The reason to keep having structures in C++ was probably to keep the language compatible with C, so it would be easier to port C code to C++, or for programmers to make that transition.

As for the usage of structures contra classes, I personally use them just like would in C, to group related variables together in a single "object".

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
Some programmer dude
  • 400,186
  • 35
  • 402
  • 621
  • 2
    There is the matter of public vs. private inheritance as well. – chris Oct 27 '12 at 20:47
  • Yes, thank you, Joachim. I mentioned the default visibility in my question. I am aware that structs exist in C++ to be backwards compatible with C. I was curious if you gained any performance benefits - if structs in C++ weren't mangled to be classes then they should simply be chunks of contiguously allocated memory - as they were in C. It would be nice if they weren't changed to be classes. Though it looks like there aren't any performance benefits to be gained from using structs in C++, which is quite unfortunate. Structs could be very useful in C++ if they didn't have the overhead. – Syndacate Oct 29 '12 at 20:24
3

In my opinion, Structures doesn't give you anything that can't be implemented by a class. But, Structures are kept in C++ in order to have backward compatibility with c

2

Undoubtedly the reason they are there is for compatibility with C. But you are right, although there are small differences between class and struct in C++ there is nothing you can do with structs that you cannot do with classes (and vice versa).

Personally I use structs only when the same declaration would be legal in C, to emphasize the C-like nature of whatever it is I'm doing.

john
  • 85,011
  • 4
  • 57
  • 81
2

They are implemented "with the same exact underlying system". In fact, you can actually declare a type using class keyword and then define it using struct keyword. Basically, they are exactly the same aside from the conceptual difference you already mentioned: default access rights.

I don't see though why would one call them "useless". The usage of the keyword became a matter of personal preference and/or coding standard. Like "use struct fro POD types", or "use struct for types with no incapsulation/access control".

With the same degree of success one can declare the built-in -> operator "useless" because of its equivalence to *+. combination.

ParkerHalo
  • 4,341
  • 9
  • 29
  • 51
AnT stands with Russia
  • 312,472
  • 42
  • 525
  • 765
  • -> is less syntax. class is a shorter word than struct. Struct is useless in C++. –  Dec 24 '14 at 13:10
  • 1
    @Jessy: "...is less syntax" is irrelevant. The roots of `->` actually date back to nascent C where it had different semantics, not equivalent to `*` and `.` combination. Wrong about `struct` as well. In order to provide public access `class` has to be combined with `public:` which makes it a lot *longer* than `struct`, not shorter. `struct` is not useless. It *massively* used in C++, which is immediately evident from the implementation of C++ standard library. – AnT stands with Russia Dec 24 '14 at 16:59
  • The public versus private issue is moot; for most classes, either you have to write private or you have to write public. I forgot to mention that struct is not even a complete word. Being useless means that something else already does what you do, better. Class is a word, and it does everything struct does. Struct has no use. –  Dec 24 '14 at 22:16
  • 1
    @Jessy: Not really. In cases when everything is supposed to be public, there's no need to write `private`. Also, aside from the default access, there's no difference between `struct` and `class`, which completely eliminates the possibility of `class` being somehow "better" (what do you mean, really?). Finally, one more time, the entire C++ standard library heavily uses `struct` when appropriate. Why do they do that, I wonder? – AnT stands with Russia Dec 25 '14 at 02:10
1

You can have the same behavior as in C, nobody forces you to use the additional "class" features. So if you understand their purpose in C, it should be understandable in C++ as well (the "C-like" portion at least).

SomeWittyUsername
  • 18,025
  • 3
  • 42
  • 85
1

As Joachim Pileborg already pointed out, classes and structures are almost the same thing in C++.

I would, however, recommend to use struct for "dumb" data holders with public members and class for properly encapsulated classes as a style convention.

Philipp
  • 67,764
  • 9
  • 118
  • 153