Aquivalent References as in C++?

Namespace rswhite4 at googlemail.com
Tue Apr 17 02:39:09 PDT 2012


On Tuesday, 17 April 2012 at 08:02:02 UTC, Namespace wrote:
> Now i have something like this. It works and manipulates 
> lvalues so that i can pass my objects by ref to except null.
>
> But is this smart?
>
> class Bar {
> public:
> 	int x;
> 	
> 	static ref Bar opCall(int x) {
> 		static Bar b;
> 		
> 		b = new Bar(x);
> 		
> 		return b;
> 	}
> 	
> 	this(int x) {
> 		this.x = x;
> 	}
> }
>
> class Foo {
> private:
> 	Bar _bar;
>
> public:
> 	int y;
> 	
> 	this() { }
> 	
> 	this(ref Bar b) {
> 		//assert(b !is null);
> 		
> 		writefln("B.x %d", b.x);
> 	}
> }
>
> Bar b = new Bar(42);
> 	
> new Foo(b); // works
> new Foo(null); // compiler error
> new Foo(Bar(23)); // works
> new Foo(Bar(25)); // works


But if I write

Bar bn;
new Foo(bn);

it works also and doesn't throw a compiler error.
To avoid this, I have to write an assert(obj !is null); again.
This really sucks. If anybody else works with my Code and puts a 
null reference to any method he gets no compiler error and wonder 
why he gets a runtime error instead.
Thats very annoying...


More information about the Digitalmars-d-learn mailing list