const behaviour
Ali Çehreli
acehreli at yahoo.com
Fri Jun 22 15:45:15 PDT 2012
On 06/22/2012 02:21 AM, Namespace wrote:
> 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;
_text is a reference to immutable(char).
>
> Bar _b;
_b is a reference to mutable Bar.
>
> 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 {
That function promises that the object will not be modified through it.
> /// <- must be const(Bar) instead of Bar
That is not entirely correct. The return type need not be const(Bar).
The problem is with what you are returning.
> return this._b;
That is the problem. To enforce its no-mutation guarantee, the function
cannot return a non-const reference to the Bar object that _b is a
reference of. The return type Bar compiles with this code:
Bar GetB() const pure nothrow {
return new Bar();
}
> }
>
> string GetText() const pure nothrow { /// <- no const(string) is
> neccessary. Why?
Because _text is a non-mutating reference.
> return this._text;
> }
> }
>
> void main() {
> Bar b = new Bar();
>
> Foo f = new Foo("foobar", b);
> }
>
> [/code]
Note that the caller can modify the string that GetText() returns by
e.g. appending to it:
string t = f.GetText();
t is a copy of _text. They currently share the same characters: "foobar".
t ~= "zar";
The act of appending does not change _text in any way. It still refers
to "foobar".
Ali
--
D Programming Language Tutorial: http://ddili.org/ders/d.en/index.html
More information about the Digitalmars-d-learn
mailing list