what was wrong with struct & class in C++?

Russell Lewis webmaster at villagersonline.com
Thu Dec 13 14:04:32 PST 2007


Walter Bright wrote:
> It's a good question. D structs are designed to represent value types, 
> and classes as reference types. The two have very different uses and 
> characteristics. C++ allows them to be mixed up together, with program 
> bugs as the usual result.
> 
> For example, the slicing problem. If you inherit from a value type, and 
> then add members, then everyone who uses the value type by value 
> "slices" off the additional members.
> 
> Virtual functions make no sense for value types, and non-virtual 
> functions are a recipe for disaster in reference types (hence the 
> exhortation to not forget to make your destructors virtual if deriving 
> from them, a lame bit of advice because base classes cannot control how 
> they are used).
> 
> In C++, one designs a class to be a reference type or a value type. 
> Interestingly, I've never once seen in documentation for a C++ class 
> whether it is supposed to be used by reference or by value.
> 
> Clearly distinguishing value types from reference types:
> 
> 1) Indicates to the user how a type is to be used
> 2) Allows for the correct semantic defaults
> 3) Eliminates whole categories of bugs that are impractical to detect in 
> C++

Hear, hear!  Good arguments, all!  It still doesn't answer why we left 
out the little star on class-reference variable assignments.  As I've 
argued before, the question of syntax is orthogonal to the question of 
legal operations.  Make value-style operations illegal for classes, and 
keep the star there, IMHO.



More information about the Digitalmars-d mailing list