const behaviour

Namespace rswhite4 at googlemail.com
Fri Jun 22 02:21:04 PDT 2012


Based to the current const discussions (once again) I wanted to 
appease my curiosity and want to ask why the following code works 
as described in the comments:

[code]

import std.stdio;

class Bar { }

class Foo {
private:
	string _text;
	
	Bar _b;
	
public:
	this(string text, Bar b) {
		this._text = text;
		this._b = b;
	}
	
	// const_behaviour.d(18): Error: cannot implicitly convert 
expression (this._b) of type const(Bar) to const_behaviour.Bar
	Bar GetB() const pure nothrow { /// <- must be const(Bar) 
instead of Bar
		return this._b;
	}
	
	string GetText() const pure nothrow { /// <- no const(string) is 
neccessary. Why?
		return this._text;
	}
}

void main() {
	Bar b = new Bar();
	
	Foo f = new Foo("foobar", b);
}

[/code]


More information about the Digitalmars-d-learn mailing list