const method and return type

Adam D. Ruppe destructionator at gmail.com
Fri Dec 13 16:31:23 PST 2013


On Saturday, 14 December 2013 at 00:24:01 UTC, Andrea Fontana 
wrote:
> Just another thought. If we have:
>
> class B;
> struct C;
>
> class A
> {
>    void method() const { ... }
>    B another_class;
>    C a_struct;
> }
>
> B is just a reference to a object, so method() should not 
> reassign it. The reference should be const, not the object 
> itself. method() should be able to call mutable another_class 
> methods. Am I wrong?

Yes, in D, all members of a const object are const too. So inside 
method() const, another_class is const. Which means 
another_class.member is also const.


More information about the Digitalmars-d-learn mailing list