const behaviour

Namespace rswhite4 at googlemail.com
Fri Jun 22 03:25:15 PDT 2012


As far as i know "int" is not immutable or const by default.
So, why work this code:

[code]
import std.stdio;

class Bar {
	
}

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

void main() {
	Bar b = new Bar();
	
	Foo f = new Foo(42, b);
}
[/code]


More information about the Digitalmars-d-learn mailing list