rebind of const class variables

Mathias LANG via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jan 20 01:36:00 PST 2015


On Tuesday, 20 January 2015 at 09:29:46 UTC, qqiang wrote:
> I am writing a tree data structure, and I have the following 
> code:
>
> ```D
> final class Node {
>     private {
> 	int val_;
> 	Node parent_;
> 	Node left_;
> 	Node right_;
>     }
>
>     @property
>     const(Node) maximum() const {
> 	auto ret = this;
> 			
> 	while (ret.right_) {
> 	    ret = ret.right_;
> 	}
> 			
> 	return ret;
>     }
> }
> ```
>
> It failed to compile and complaint that `cannot modify const 
> expression ret`。
>
> Since `ret` is just a binding to a const class object, why 
> can't I rebind it to another const class variable?
>
> Must I use pointers to cope with this?
>
> Thx

You are looking for 
http://dlang.org/phobos/std_typecons.html#.Rebindable


More information about the Digitalmars-d-learn mailing list