Re: Read-only from the outside
Frederick Gotham wrote:
Maybe it would be nice if we could specify that a member object should be
non-const within the class, but appear const to the outside world...
perhaps by combining "const" and "export":
template<class T>
class Arb {
public:
T const export obj;
void Func()
{
obj = 6; /* No problem */
}
};
int main()
{
Arb obj;
obj.obj = 6; /* Compile ERROR: const violation */
}
Perhaps this could be extended to such elaborate definitions as:
T const export *const export obj;
T const export *const obj;
T *const export obj;
T const export *obj;
Excuse me; But I found it ridiculous...
That's an encapsulation flaw, anyway.
A much better alternative would be to provide an accessor!
And, if you really want to change the language... You'd better propose
properties.
Properties give all the syntaxic sugar you want, and a much better
encapsulation.
And even much more syntaxic sugar.
Though, personally, I think that even properties don't worth the
language change.
I find that.
my_obj.my_field(4);
int i=my_obj.my_field();
Is as good as:
my_obj.my_field=4;
int i=my_obj.my_field;
And, it is even possible to make accessors return a reference to *this
my_color.red(42).green(68).blue(4);
Is more compact, and pretty than:
my_color.red=42;
my_color.green=68;
my_color.blue=4;
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]