Aquivalent References as in C++?

Timon Gehr timon.gehr at gmx.ch
Mon Apr 16 14:34:34 PDT 2012


On 04/16/2012 11:25 PM, Namespace wrote:
> Hi,
>
> I have a few questions about D and could use some help. For
> instance, how can i rebuild such a behavior?
>
> class Foo {
> public:
> Foo(const Bar& b) {
>
> At C++ you can ensure that a reference is requested and must not
> null.
> Nevertheless lvalues are functional, which means Foo(Bar(42))
> would operate just as good as
> Bar b(42);
> Foo(b);
>
> The same procedure doesn't seem to operate with const ref,
> instead you can only use ref. But in this case Ivalues doesn't
> operate.
> Because of the fact that in D all objects are indication of
> references I thought that I simply didn't need a storage class.
> But then I recognized that at my D equivalent…
>
> class Foo {
> public:
> this(Bar b) {
>
>
> …null can also be passed.
>
> That case I would like to prevent, but at the same time allow
> Ivalues. How does that work in D classes?
> As far as I know this operates with structs, but shouldn't it be
> possible with classes and objects too?
>
> Would be really nice if anyone had an advice for me.

You could do this:

this(Bar b)in{assert(b !is null);}body{


More information about the Digitalmars-d-learn mailing list