Aquivalent References as in C++?

bearophile bearophileHUGS at lycos.com
Mon Apr 16 14:39:14 PDT 2012


Namespace:

> how can i rebuild such a behavior?
>
> class Foo {
> public:
> Foo(const Bar& b) {

In D struct instances are values, and you can manage them as 
values, or as a pointer to value (or pointer to pointer to value, 
etc). In D class instances are always managed by reference, and 
such references, like pointers, can be null (and emplace() is 
supposed to allow in-place allocation of a class instance).

So in normal D code you can't write D code equivalent to that C++ 
code. Some possibilities:

class Foo {}
void bar(Foo f) {}
void bar(const Foo f) {}

struct Baz {}
void spam(Baz b) {}
void spam(in Baz b) {}
void spam(ref Baz b) {}
void spam(Baz* b) {}
void spam(const Baz* b) {}
void spam(const ref Baz b) {}
void spam(immutable ref Baz b) {}
There is also inout, out, etc.

Lot of people (me too) have asked non-null class references (and 
pointers) in D. We are now able to @disable some constructors...

Bye,
bearophile


More information about the Digitalmars-d-learn mailing list