//TODO: professional stuff of software engineerĀ 1001010
C++ const ref member

I know people bag on c++ (for many valid reasons) but it still amazes me to learn something new: read only members using const references. It’s even better when a c++ non-fan teaches you about it. Thanks Nico!

class Test
{
public:
  Test() : readOnlyVersion(m_writableVersion)
  {}
  const SomeType & readOnlyVersion;
private:
  SomeType m_writableVersion;
};

Never having a need, I had always filtered out reference data members – I just categorized them with static members and their special initialization. Now that I see a use case, I’m basking in the “Ah that makes total sense” moment.

About

Categories: C++, Code Tags: ,