Date: Mon, 16 Dec 2024 07:21:01 -0600
> I propose two new access specifiers to make this process easier. First
one is "exposed private". Member variables defined after this specifier
would allow anyone to access these variables as const, but only owner class
to modify them.
I could see this being useful, e.g. in testing, but also I’m not convinced
it makes a big enough difference to warrant first-order language support.
This would be a big change, both in terms of language functionality and
also OOP philosophy. Are there any other languages that have something like
this? I think it’s hard to make an argument for a language feature here
when getters work well and give you more control. The boilerplate issue is
solved with tooling, e.g. Clion can generate getters/setters for you. Being
able to refer to a field with the same name is nice but get_foo() isn’t a
big deal.
Cheers,
Jeremy
On Sun, Dec 15, 2024 at 23:08 James via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> In C++, it’s common to encounter situations where a member variable needs
> to be fully controlled by its owner class but also readable from outside
> the class. The typical solution is to define a public getter for the
> variable:
> ```cpp
> class Foo
> {
> int value = 0;
> public:
> int get_value() const { return value; }
> };
> ```
>
> I propose two new access specifiers to make this process easier. First one
> is "exposed private". Member variables defined after this specifier would
> allow anyone to access these variables as const, but only owner class to
> modify them.
> The other specifier is "exposed protected", it's the same as before, but
> also allows derived classes to modify these variables.
>
> So previous code could just be rewritten as following:
> ```cpp
> class Foo
> {
> exposed private:
> int value = 0;
> };
> ```
>
> It's less boilerplate and you get to refer to the variable with its
> original name.
> ```cpp
> Foo foo;
> int value = foo.value; // ok
> foo.value = 15; // error, can't modify const variable
> ```
>
> Note: I'm not sure about naming and I'm open to ideas. Also the exact
> thing I just specified might contain issues, but you get the idea.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
one is "exposed private". Member variables defined after this specifier
would allow anyone to access these variables as const, but only owner class
to modify them.
I could see this being useful, e.g. in testing, but also I’m not convinced
it makes a big enough difference to warrant first-order language support.
This would be a big change, both in terms of language functionality and
also OOP philosophy. Are there any other languages that have something like
this? I think it’s hard to make an argument for a language feature here
when getters work well and give you more control. The boilerplate issue is
solved with tooling, e.g. Clion can generate getters/setters for you. Being
able to refer to a field with the same name is nice but get_foo() isn’t a
big deal.
Cheers,
Jeremy
On Sun, Dec 15, 2024 at 23:08 James via Std-Proposals <
std-proposals_at_[hidden]> wrote:
> In C++, it’s common to encounter situations where a member variable needs
> to be fully controlled by its owner class but also readable from outside
> the class. The typical solution is to define a public getter for the
> variable:
> ```cpp
> class Foo
> {
> int value = 0;
> public:
> int get_value() const { return value; }
> };
> ```
>
> I propose two new access specifiers to make this process easier. First one
> is "exposed private". Member variables defined after this specifier would
> allow anyone to access these variables as const, but only owner class to
> modify them.
> The other specifier is "exposed protected", it's the same as before, but
> also allows derived classes to modify these variables.
>
> So previous code could just be rewritten as following:
> ```cpp
> class Foo
> {
> exposed private:
> int value = 0;
> };
> ```
>
> It's less boilerplate and you get to refer to the variable with its
> original name.
> ```cpp
> Foo foo;
> int value = foo.value; // ok
> foo.value = 15; // error, can't modify const variable
> ```
>
> Note: I'm not sure about naming and I'm open to ideas. Also the exact
> thing I just specified might contain issues, but you get the idea.
> --
> Std-Proposals mailing list
> Std-Proposals_at_[hidden]
> https://lists.isocpp.org/mailman/listinfo.cgi/std-proposals
>
Received on 2024-12-16 13:21:14