TITLE: bjarne on struct versus class (Newsgroups: comp.lang.c++, 8 Jul 97) STUPY: _stupy@nospam.com > > > > You must remember that C was out first. structs were the norm then. In > > an effort to conform and be compatible with current specs, Bjarne and > > the gang just used another name for the same thing and extended from > > there. WILDEN: Mark@mWilden.com > But why was this necessary? Since C++ extended structs (as well as > adding classes), I don't see the point. BJARNE: bs@research.att.com (Bjarne Stroustrup) It is not necessary, just usefull. 'struct' and 'class' differs only in the default access to members. There is, however, a vast diffference between a type with a hidden state manipulated by a well-defined set of operations and a data structure where the data members are part of the public interface and can be manipulated by any code - even if a set of member functions is provided for convenience. The keyword 'class' helped people focus on that distinction and 'class' - even though it is just a reminder - has been most helpful to many people. I have also found that teaching people a feature that is not distinguished by a keyword can be surprisingly hard. My best example is abstract classes. I think, I would have had an easier time and succeeded far sooner, had I had a keyword 'abstract' or 'pure' to remind people. WILDEN: > Personally, I've started going from > > class Class : public Base { > public: > // > private: > // > }; > > to simply > > struct Class : Base { > // > private: > // > }; > > Since most of my derived classes use public derivation, and since I > believe in putting public members first in class definitions, this saves > some typing. BJARNE: My ideal was (and is) that everything should be private unless explicitly declared to be accessible. `struct' encourages people to leave data members public. Personally, I try to use 'struct' only for classes that really are just datastructures and for which I can't specify a useful invariant. You can find the answers to most of this kind of 'why' questions in "The Design and Evolution of C++".