2 ms·
Both the original example and your translation exhibit poor API design: it's an antipattern to return mutable references to member variables. This would remove
by blub 10y ago
Both the original example and your translation exhibit poor API design: it's an antipattern to return mutable references to member variables.
This would remove all encapsulation, which is probably the most important principle of OOP.
Nevertheless, a better solution was pointed out in an answer on the blog and by humanrebar.
- erikdesjardins 10y agoThe original example does not return a mutable reference; in Rust you must explicitly opt in to mutability. Compare [1] and [2]. [1]: https://play.rust-lang.org/?gist=78e7af18c6fc7d37d9df96be63ac23d8 https://play.rust-lang.org/?gist=78e7af18c6fc7d37d9df96be63a... [2]: https://play.rust-lang.org/?gist=2072a18b674685f20c9d1504a8829a3d https://play.rust-lang.org/?gist=2072a18b674685f20c9d1504a88...
- blub 10y agoOh ok, my bad. Then the C++ translation above should probably be a const& in C++, which is acceptable. The returned reference could in that case be stored, but it's risky and a bad practice.