Multiple Inheritance, Mixins, and Constructors

Chris Williams littleratblue at yahoo.co.jp
Tue May 19 19:02:01 PDT 2009


Ary Borenszweig Wrote:

> 
> You can do this:
> 
> template TFoo() {
> 	// Never use this variable directly :-P
> 	SomeObject _s_o;
> 
> 	SomeObject s_o() {
> 		if (_s_o is null)
> 			_s_o = new SomeObject();
> 		return _s_o;
> 	}
> 
> 	// And if you want to make it assignable too
> 	SomeObject s_o(SomeObject x) {
> 		_s_o = x;
> 	}
> 	
> 	void bar() {
> 		s_o.x = 10;
> 		// code
> 		// ...
> 	}
> }

Well I ended up converting my object to a struct, which solved the issue. Your solution would probably work, but for what I'm doing, I'd rather not incur the overhead of a branch and (possible) function call for every access of the object.

It seems like object initialization outside of a method should be magically moved to the top of the constructor. But ah well.

Thank you.


More information about the Digitalmars-d-learn mailing list