Feature request - simpler constructors
James Dennett
jdennett at acm.org
Fri Sep 21 19:50:24 PDT 2007
Janice Caron wrote:
> I tried to look at tango source code, but I can't seem to connect to
> dsource.org right now. Hopefully that's a transient problem.
>
> I know that Sun's official Java tutorial (see
> http://java.sun.com/docs/books/tutorial/java/javaOO/constructors.html)
> tells you to do exactly what I've been talking about. I quote:
>
> public Bicycle(int startCadence, int startSpeed, int startGear) {
> gear = startGear;
> cadence = startCadence;
> speed = startSpeed;
> }
>
> C++'s syntax for doing this is rather complicated and confusing, but I
> should add that the full blown C++ mechanism of allowing member
> variables to be assigned with arbitrary expressions in not needed in
> D. It is needed in C++ because everything is passed by copy, meaning
> that copy constructors and destructors have to be run as values are
> copied to and from functions. That would not be needed in D because
> classes are copied by reference, and structs require no copy
> constructor. Thus
>
> : (x,y,z)
>
> would suffice for us. I think it would be marvellous.
It's nothing to do with the need to copy by-value objects
in C++, and everything to do with the difference between
initialization and assignment. Many C++ entities cannot
be assigned to (references, const objects, objects of
class types which don't allow assignment), and C++ needs
syntax to initialize them. The body of the constructor
is too late.
-- James
More information about the Digitalmars-d
mailing list