D
David Gausebeck
The inability to denote data members as read-only in C++ is something
which has annoyed me (slightly) for a while now. You can always get
around it with accessor methods that return const references, but
classes with huge lists of accessors often seem awkward/ugly.
So I finally sat down and thought of a syntax I'd like to see as a
fix. I certainly don't expect to ever see it introduced, but it's
interesting to think about.
The syntax is to add a new keyword, "expose", which can only be used
in class declarations. After expose, you redeclare an existing class
member with a greater degree of const-ness, at a different protection
level. It might be necessary to only expose a member after that
member's declaration, but for this example I'll assume you can use it
before:
class Foo {
public:
// public can read the string contents
expose const char* const str;
protected:
// derived classes can modify the string contents
expose char* const str;
private:
// only Foo can change str itself
char* str;
};
My questions for the group are:
1) Would compilers have any problem handling this syntax and its
effects? It seems like it would be relatively easy to handle, but
I don't know all that much about compilers.
2) Are there any flaws with the syntax or its effects? i.e. would it
lead to any ambiguous situations or have undesirable side effects?
3) Do you think that, if this syntax were magically added to C++, it
would be a good/useful thing or a bad/useless thing?
-Dave
which has annoyed me (slightly) for a while now. You can always get
around it with accessor methods that return const references, but
classes with huge lists of accessors often seem awkward/ugly.
So I finally sat down and thought of a syntax I'd like to see as a
fix. I certainly don't expect to ever see it introduced, but it's
interesting to think about.
The syntax is to add a new keyword, "expose", which can only be used
in class declarations. After expose, you redeclare an existing class
member with a greater degree of const-ness, at a different protection
level. It might be necessary to only expose a member after that
member's declaration, but for this example I'll assume you can use it
before:
class Foo {
public:
// public can read the string contents
expose const char* const str;
protected:
// derived classes can modify the string contents
expose char* const str;
private:
// only Foo can change str itself
char* str;
};
My questions for the group are:
1) Would compilers have any problem handling this syntax and its
effects? It seems like it would be relatively easy to handle, but
I don't know all that much about compilers.
2) Are there any flaws with the syntax or its effects? i.e. would it
lead to any ambiguous situations or have undesirable side effects?
3) Do you think that, if this syntax were magically added to C++, it
would be a good/useful thing or a bad/useless thing?
-Dave