returning constant references from class methods

ag0aep6g via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Jul 19 05:33:53 PDT 2016


On 07/19/2016 02:30 PM, celavek wrote:
> Hi,
>
> I'm trying the following code:
>
> class counter
> {
> public:
>
>      final ulong[char] nucleotide_counts () const
>      {
>          return cached_counts;
>      }
>
> private:
>      ulong[char] cached_counts;
> }
>
> void main()
> {
> }
>
> I get the following error from the compiler:
>
>     Error: cannot implicitly convert expression (this.cached_counts) of
> type const(ulong[char]) to ulong[char]
>
> I tried making the class variable constant, I tried the const in the
> return type but I can't seem to get it right.
>
> I would like to return a constant reference to the internal associative
> array - much like in C++. How would I do that in D?

     final const(ulong[char]) nucleotide_counts () const
     {
         return cached_counts;
     }


More information about the Digitalmars-d-learn mailing list