There are people who try to use the 'struct' keyword to mean various things. For example, some people attempt to use struct
to specify POD-ness or to be only data classes (classes with only public member variables and no functions).
In my opinion this is a serious mistake and all the reasons I've heard for it are flawed. For one thing, a keyword like struct
is not sufficient to express the conditions they are attempting to use it to document. Since the keyword actually doesn't mean anything in particular people either accept this, or attempt to assert their own idea of what it should mean. There are too many incompatible opinions on what struct
should mean in code for it to document any of them. You want to document things like, "This object must always be a POD," as clearly as possible instead of relying on obscure team standard details that can be difficult to remember. So, in order to document such a thing the only sufficient thing, honestly, is, // This object must always be a POD
. It's clear, simple, and can't be misinterpreted by anyone who knows what a POD is, and this is well defined.
The best argument I've ever heard for using the keyword class
instead of struct
is that you should prefer private access to public access, revealing only that which is necessary. In other words, the "default" kind of inheritance and access exposure should be private until it is shown that more access is necessary. I agree with this opinion but I don't agree it is necessary to use the struct keyword to adhere to this principle.
Whatever one decides is going to be fairly arbitrary. Reasons can later be asserted for the decision but I find people who've chosen one or the other aren't easy to convince. I switched from 'class' to 'struct' because I read too many books on modern C++ and too much boost code; you'll rarely see the 'class' keyword in either. Truth be told, it doesn't matter; follow the standards of the team you are in at the time. In my opinion, adding a keyword that is essentially meaningless was a mistake.
Edit: Although, I do have to say that another reason I dislike using class
is that it means too many things even while it also means nothing in particular. For example, you can use class
instead of typename
in template parameter specifications. Even if you use the class
keyword you can pass any type and not necessarily any kind of class (like int
). The only time I like to use the class
keyword is where it actually does mean something and where you actually absolutely have to: specifying template template parameters.