Re: initonly array member as an lvalue
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Wed, 28 Nov 2007 10:17:43 -0600
"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospam> wrote in message
news:1A6D5254-8C82-4E3E-B1E1-DF8FD85D2F3B@xxxxxxxxxxxxxxxx
Right. I see what you mean, but from the readonly (C#) docs:
"...assignments to the fields introduced by the declaration can only occur
as part of the declaration or in a constructor in the same class."
He isn't assigning to the field, he's performing member access on the object
referenced by the field.
It's like having
class C
{
bool* const p; // p must be assigned in constructor, p[0] can be
assigned anywhere
};
The workaround would be:
ref class C
{
initonly array<bool>^ b;
C() : b(gcnew array<bool>(8)) {}
void doit() { /* b[0] = false; */ array<bool>^ bprime; bprime[0] =
false; }
};
and
"The readonly keyword is different from the const keyword. A const field
can only be initialized at the declaration of the field. A readonly field
can be initialized either at the declaration or in a constructor.
Therefore, readonly fields can have different values depending on the
constructor used."
That reads to me just like initonly and const in C++. That's why I wonder
if C# should allow you to change readonly variables later.
It doesn't read like it should just apply to the reference.
Mark
--
Mark Salsbery
Microsoft MVP - Visual C++
"lee.crabtree" <lee.crabtree@xxxxxxxxx> wrote in message
news:5334ee6e-bb71-4f58-af3b-a58f9fafd627@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I think it's more likely that the association of 'initonly' to
'readonly' is erroneous in some regard. As far as I can tell,
'readonly' is only supposed to prevent the REFERENCE from being
overwritten. In that sense, a List<> marked 'readonly' will allow
adding, removing, and clearing from the list, but setting the list to
null or trying to re-new it would be an error. In that sense, it's
similar to what I remember (from my long-past days of C++) a 'const'
pointer being used for. The VALUE is changeable, it's the REFERENCE
that isn't.
.
- Follow-Ups:
- Re: initonly array member as an lvalue
- From: Ben Voigt [C++ MVP]
- Re: initonly array member as an lvalue
- References:
- initonly array member as an lvalue
- From: lee.crabtree
- Re: initonly array member as an lvalue
- From: Mark Salsbery [MVP]
- Re: initonly array member as an lvalue
- From: lee.crabtree
- Re: initonly array member as an lvalue
- From: Mark Salsbery [MVP]
- Re: initonly array member as an lvalue
- From: lee.crabtree
- Re: initonly array member as an lvalue
- From: lee.crabtree
- Re: initonly array member as an lvalue
- From: lee.crabtree
- Re: initonly array member as an lvalue
- From: Mark Salsbery [MVP]
- initonly array member as an lvalue
- Prev by Date: Re: Parameters to CreateFile
- Next by Date: Re: initonly array member as an lvalue
- Previous by thread: Re: initonly array member as an lvalue
- Next by thread: Re: initonly array member as an lvalue
- Index(es):
Relevant Pages
|