What is the advantage of using a struct
in C++ when you have class
? I cannot find any literature on the subject. I have been told to just use class and is the correct
Asked
Active
Viewed 62 times
0

TemplateRex
- 69,038
- 19
- 164
- 304

user3286701
- 7,963
- 3
- 12
- 4
-
None, there is no real difference, it is purely a matter of convention, but you might get flooded by answers suggesting otherwise :-) – juanchopanza Feb 08 '14 at 09:34
-
Have you considered investigating the difference between structs and classes in C++ and drawing your own conclusions? – Oswald Feb 08 '14 at 09:35
-
The only difference is that with `class`, members are made private automatically whereas with `struct` they are public (before specifying any of `public:`, `private:`, `protected:` that is). – Aleph Feb 08 '14 at 09:35
-
@AnotherTest Sigh. No, that is not the *only* difference. This always happens when this question gets asked. – juanchopanza Feb 08 '14 at 09:36
-
@juanchopanza then what are the other differences – Aleph Feb 08 '14 at 09:37
-
@AnotherTest It is explained in the duplicate. – juanchopanza Feb 08 '14 at 09:37
-
@juanchopanza link is gone, but I see: difference in derived types as well. Thanks, didn't know about this. – Aleph Feb 08 '14 at 09:39
-
@AnotherTest You can find the link at the top of the question now. It is worth a read, if only because there are many out there who have the impression you cannot do as much with `struct` as with `class`. – juanchopanza Feb 08 '14 at 09:41
-
@juanchopanza Unfortunately the _other differences_ aren't explained in the accepted answer of the dupe, but in the second highvoted one. – πάντα ῥεῖ Feb 08 '14 at 09:49
-
@πάνταῥεῖ Actually, the accepted answer *does* mention it, "that structs have default public members and **bases**", and the answer you refer to gets that part wrong (or at least, is not clear enough), and then talks about a "real difference" that is just down to convention. It is a very bad answer! – juanchopanza Feb 08 '14 at 09:56
-
@juanchopanza THX for enlightening, didn't spot that at the 1st glance. – πάντα ῥεῖ Feb 08 '14 at 09:57