1.2 A Keyword Distinction
Because C++ strives to maintain (as close as possible) language compatibility with C, C++
is considerably more complicated than it would otherwise be. For example, overloaded
function resolution would be a lot simpler if there were not eight flavors of integer to
support. Similarly, if C++ were to throw off the C declaration syntax, lookahead would not
be required to determine that the following is an invocation of
pf
rather than its definition:
// don't know if declaration or invocation
// until see the integer constant 1024
int ( *pf )( 1024 );
On the following declaration, lookahead does not even work:
// meta-language rule:
// declaration of pq, not invocation
int ( *pq )( );
A meta-language rule is required, dictating that when the language cannot distinguish
between a declaration and an expression, it is to be interpreted as a declaration.
Similarly, the concept of a class could be supported by a single class keyword, if C++ were
not required to support existing C code and, with that, the keyword struct. Surprisingly,
one of the most-asked questions of C programmers moving to C++ (once the questions
about performance have been put aside) is when, if ever, should one use a struct declaration
rather than a class declaration when writing a program using C++?
In 1986, my answer to this question was an unequivocal "never." In both the first and
second editions of my C++ Primer, the keyword struct does not appear in the text, except
in Appendix C, which, logically enough, discusses the C language. At that time, this was one
of those small philosophical points that one necessarily does not point out. However, it is
one from which one gains some small (admittedly quite small) satisfaction when, well, when
it is pointed out, usually as a question: "Hey, did you know? Struct. The keyword. It's not
used anywhere.? But as a colleague of mine at Bell Laboratories indirectly pointed out to
me, even the smallest philosophical point can have a human cost. For example, a C
programmer in his group, anxious to learn C++, was quite distressed to find struct absent
from my book. Apparently its inclusion would have provided a transitional lifeline to make
the programmer's rocky ascent less bruising. So much, then, for philosophy.
Keywords, Schmeewords
One answer to the question of when, if ever, you should use a struct declaration rather
than a class declaration then, is whenever it makes one feel better.
Although this answer does not achieve a high technical level, it does point out an important
distinction that it is important not to make: the keyword struct by itself does not necessarily
signify anything about the declaration to follow. We can use struct in place of class, but still
declare public, protected, and private sections and a full public interface, as well as specify
virtual functions and the full range of single, multiple, and virtual inheritance. Back in the
"early days," it seemed everyone would spend a full 10 minutes of a one-hour introductory
talk on C++ distinguishing the nondistinction between
class cplus_plus_keyword {
public:
// mumble ...
};
and its C equivalent
struct c_keyword {
// the same mumble
};
When people and textbooks speak of a struct, they mean a data aggregate without private