C++ Logo

std-proposals

Advanced search

Re: [std-proposals] New access specifiers

From: Oskars Putans <o.putaans_at_[hidden]>
Date: Mon, 16 Dec 2024 12:11:07 +0200
What if you used

class Foo {
private:
  int value;
public:
  const int& readOnlyValue = value;
};

as a way of exposing a const version?

It might not work in all use cases as it does remove
std::is_trivially_assignable trait from
the class, but this gets rid of an explicit getter and reduces boilerplate.
If you want to support assignment, you need to write your own overloads.
This method is slightly shady, however, as other developers might make
the assumption that this value won't change. I would rather use getters for
this, but this is one alternative you can consider. Realistically, as a
proposal to core cpp, there's a very low likelihood that this would make
the cut.

Received on 2024-12-16 10:11:21