Howard said:
Backwards compatibility, both with C and with older C++ code, I assume.
In my first C++ compiler (Turbo C++ 2, I think), the keyword struct meant
a purely C-style struct, what we call a POD ("plain old data") structure
today. No member functions, no inheritance, simply public data. The
class keyword in that software indicated the new C++-style definition,
with member functions and inheritance allowed.
I (and many other programmers, I'm sure) still tend to program in that
fashion, using struct only for pure POD data holders, and class for
everything else.
Then we get this:
class Foo: public Bar
{
public:
void method();
private:
int x;
};
From the top: Public inheritance is more popular than private inheritance
(despite the latter is slightly more robust). But classes default to private
so we have to write 'public Bar' on most base classes.
Then, we should put the public methods at the top of the class, to document
"use me like this". And we put the privates down at the bottom, to document
"please get bored before you read this far".
So, by giving 'class' default 'private' access, as a prod to remind us to
use good design style, we have to write 'public' twice as often as we should
have.
struct Foo: Bar
{
void method();
private:
int x;
};
That's cleaner, except we should use 'struct' to mean "plain ole data
bucket", and not let our colleagues think that's not a class.
#define CLASS_ struct
CLASS_ Foo: Bar
{
void method();
private:
int x;
};
And that's just evil (and hard to grep for).