J
James Kanze
I got it from Bjarne Stroustrup, in The C++ Programming
Language. It's not poor style.
Style is a matter of taste. I know a lot of people who consider
it poor style. I also know some (a minority, but it does
include some experts) who use it systematically: struct if all
members are public.
The only absolute rule here is to use whatever style the other
programmers in your company are using.
Furthermore, it is very popular, which is the exact opposite
of the main claim from your previous post (which you snipped).
I don't know how popular it is. Stroustrup uses it, but I've
not seen many others recommending it. The most frequent rule
seems to be either "use struct for PODS, class for everything
else", or "use struct if there are data members and they are
public, class for everything else". And there are doubtless
other reasonable rules.
"Save" a few keystrokes? That presumes that writing class,
followed by public:, is somehow the natural order of things,
and that using struct deviates from that order. That's
ridiculous; if you don't have any private members, why use a
keyword whose sole purpose is to make a struct's default
access level private?
Because it is the accepted convention where you work.
Like most people (I think), I always start my classes with the
public members. (This is really a questionable policy, but so
many places I've worked in have had it that I tend to do it
automatically.) And still use class, although the first
elements are public. For better or for worse, the words
"struct" and "class" speak to the reader, and you want to use
one which tells the reader the truth, when interpreted as he
interprets it.
That's one possible convention, being your favorite does not
make it "about the only useful reason."
class foo
{
public:
// ...
};
Is more verbose, and IMO no clearer, than:
struct foo
{
// ...
};
Clarity depends on the local conventions. If the local
convention says that "struct" means PODS, then using it for
anything else is less clear.
In the first place, that's a useless warning, since there is
no difference between a class and a struct. They're the same
thing. The keywords just introduce different default access
levels, when used to begin a definition.
In the second place, your compiler may not be configured in a
sane way. GCC, with the warnings cranked up, produces no such
warning, nor should it.
Off hand, I can't find a compiler that warns about it, even with
the warnings cranked up.
In the very distant past, VC++ 6 didn't just warn, it treated it
as an error (IIRC). But seriously, VC++ 6 is decidedly
pre-standard, and while I don't think one should constantly run
to use the latest version of every compiler, there's really no
excuse for going to the opposite extreme, and using compilers
that are more than ten years old (and no longer supported by
their vendor).
In the third place, if you really just want to be consistent,
the thing to change is the forward declaration, not the
definition. The following two declarations are semantically
identical:
class base;
struct base;
The problem is that if the rule depends on the contents of the
class, and you change the contents (in a way that should be
transparent to the user), all client code has to be modified.
In the fourth place, forward declarations smell bad. There's
rarely any good reason to start declaring code to work with
classes whose definitions have not even been seen.
Forward declarations reduce coupling and dependencies. They
should be used whenever possible. (Of course, if you use the
compilation firewall idiom, you don't even need the forward
declarations.)
It doesn't need to be "fixed," because it isn't broken. If
you need to work with a type that hasn't been defined yet, you
can easily give it a name by making it a template parameter.
That's the right thing to do. Then, you don't have to care
whether it's a class name, a typedef, or a primitive type.
But you have introduced significant extra complexity, for
nothing. And in the absense of export, significant extra
coupling. Templates are something to be avoided (except when
the alternatives are worse, of course).