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

Gregor Richards Richards at codu.org
Wed Dec 12 12:24:44 PST 2007


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



More information about the Digitalmars-d mailing list