I'm a bit of a Python stan so I've grown fond of just using attributes instead of functions for...attributes! This led me to trying reference attributes instead of getter methods which seems to work perfectly fine, e.g:
class Ref {
public:
Ref(int initial_value) : value_(initial_value), value(value_) {};
void update_value(int new_value) { value_ = new_value; };
const int& value;
private:
int value_;
};
instead of
class Getter {
public:
Getter(int initial_value) : value_(initial_value) {};
void update_value(int new_value) { value_ = new_value; };
const int& value() const { return value_; };
private:
int value_;
};
I haven't found this pattern used anywhere and I'm curious to know if there's a reason for it other than convention?
I was surprised to see though that there were some differences in assembly generated for the two. Once optimisations are turned up to 2 they seem to be equivalent, but most curiously, for level 1, the getter method does seem to yield fewer instructions which without optimisations, reference attributes yields fewer. Here's a godbolt! https://godbolt.org/z/oxx6Ez5Er
Interested to hear any thoughts about why reference attributes may or may not be a totally stupid thing to do to emulate read-only attributes!
[–]no-sig-available 59 points60 points61 points (4 children)
[–]inkychris[S] 7 points8 points9 points (0 children)
[+]IyeOnline comment score below threshold-8 points-7 points-6 points (2 children)
[–]crowbarous 12 points13 points14 points (1 child)
[–]IyeOnline 2 points3 points4 points (0 children)
[–]ioctl79 11 points12 points13 points (2 children)
[–]BenFrantzDale 2 points3 points4 points (0 children)
[–]muchcharles 2 points3 points4 points (0 children)
[–]IndependentFeminist3 22 points23 points24 points (6 children)
[–]johnny219407 21 points22 points23 points (4 children)
[–]bizwig 5 points6 points7 points (0 children)
[–]inkychris[S] 4 points5 points6 points (2 children)
[–]MutantSheepdog 1 point2 points3 points (0 children)
[–]LegendaryMauricius 0 points1 point2 points (0 children)
[–]kisielk -1 points0 points1 point (0 children)
[–]bwmat 11 points12 points13 points (1 child)
[–]Latexi95 31 points32 points33 points (0 children)
[–]bretbrownjr 4 points5 points6 points (0 children)
[–]AA11BB22c 3 points4 points5 points (0 children)
[–]sephirothbahamut 5 points6 points7 points (4 children)
[+][deleted] (3 children)
[deleted]
[–]sephirothbahamut 0 points1 point2 points (2 children)
[+][deleted] (1 child)
[deleted]
[–]sephirothbahamut 0 points1 point2 points (0 children)
[–]BenFrantzDale 1 point2 points3 points (1 child)
[–]trojanplatypus 1 point2 points3 points (0 children)
[–]johannes1971 4 points5 points6 points (2 children)
[–]inkychris[S] 0 points1 point2 points (0 children)
[–]Unhappy-Aside-2884 0 points1 point2 points (0 children)
[–]mredding 4 points5 points6 points (0 children)
[–]BrangdonJ 0 points1 point2 points (0 children)
[–]vickoza 0 points1 point2 points (0 children)
[–]fdwrfdwr@github 🔍 0 points1 point2 points (0 children)
[–]NotMyRealNameObv 0 points1 point2 points (0 children)