digitalmars.D.learn - returning constant references from class methods
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?
Jul 19 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; }
Jul 19 2016
On Tuesday, 19 July 2016 at 12:33:53 UTC, ag0aep6g wrote:final const(ulong[char]) nucleotide_counts () const { return cached_counts; }OMG! I'm so blind. Never thought of trying the obvious way. Thank you
Jul 19 2016
On Tuesday, 19 July 2016 at 12:30:49 UTC, celavek wrote:final ulong[char] nucleotide_counts () const { return cached_counts; }BTW you can find enumap useful https://forum.dlang.org/post/hloitwqnisvtgfougncf forum.dlang.org if you want to have small associative arrays with enum keys.
Jul 19 2016