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

Martin Hinsch m.hinsch at rug.nl
Wed Dec 12 13:02:41 PST 2007


On Wed, 12 Dec 2007 12:24:44 -0800, Gregor Richards wrote:

> BC wrote:
>> I thought it was a great idea to allow both value semantics and pointer
>> semantics for the same types in C++ by the simple addition of a * or &.
>> What was wrong with that that needed to be fixed in D? GC could have
>> been done without changing this. Now in D template code doesn't know
>> which semantics apply and structs can't inherit. If you write something
>> as a struct and then realise you need inheritance you're stuck. D has so
>> many good ideas it's a shame to see it broken in this way...
> 
> 
> No. No no no no no. I hate seeing this argument.
> 
> structs are a shim. They are a way of getting lower-level access to the
> organization of memory.
> 
> In C++, this is ANYTHING but simple:
> 
> class A {
> ...
> }
> }
> void someidiotfunction(A a);
> void someidiotfunction2(A *a);
> void someidiotfunction3(A &a);
> 
> You now have three ways of passing an "object", with different semantics
> and different possible results!
> 
> In proper object oriented design, objects are contexts. It makes no sense
> to duplicate contexts. Contexts are an abstraction of an environment in
> which to perform actions, and it makes no sense to duplicate that
> environment whenever you happen to pass it from one function to another
> (oh except when you don't).
> 
> D is not the problem. C++'s insistence on forcing details of
> implementation down the users' throat is the problem.
> 
>   - Gregor Richards

I don't mean to be offensive, but I totally don't see the point. Of course
C++ structs (being inherited from C) are too low-level. Still, I would
argue that this is a property of C++ not of value-semantics. And who
decides a) what "proper object-oriented design" means, b) that it's
inherently a great thing and c) that it is tied to reference semantics?
IMO it makes perfect sense to treat objects as just a more complicated
kind of value. Look at the following code:

int a=1;
a++;
int b = a + 10;

In this piece of code the variable a is used once as a reference (i.e.
modified in place) and once as a value (i.e. copied). In the same way it
can make sense for "real" objects to use them as values in some operations
and as references in others. You can easily make up a way to specify which
way to use a function arg (like, say, 'in' and 'inout') without having to
know anything about values or references.
To sum it up - I think although C++ structs + pointers + references is
messy, that doesn't mean that OOP and value vs. reference semantics aren't
completely orthogonal concepts.
And by the way this is the main point keeping me from using D (and the
fact that the gcc frontend is so slow in catching up). Since I started
to read the newsgroup I got the impression that the special syntactic
treatment of object references has been *the* major hurdle for many of the
more difficult/controversial language extensions (const comes to mind).

cheers
Martin

-- 
Martin Hinsch

m.hinsch at rug.nl
+31 50 363 8097

CEES Centre for Ecological and Evolutionary Studies
Biologisch Centrum
Kerklaan 30
Postbus 14
9750 AA Haren




More information about the Digitalmars-d mailing list