Re: initonly array member as an lvalue
- From: "Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx>
- Date: Wed, 28 Nov 2007 13:02:29 -0600
"Mark Salsbery [MVP]" <MarkSalsbery[MVP]@newsgroup.nospam> wrote in message
news:2191D9B4-66A5-4A8D-8D79-55F6639D0C0C@xxxxxxxxxxxxxxxx
Right. You can get around const with casts too.
But why would const or initonly be used on variables one wants to change
later?
What cast? There is no cast.
The reference marked as initonly is not being changed, it always holds the
address of the same object. The data within that object could change. This
could, for example, allow a gc optimization because the lifetime of the two
objects are equal and therefore the objects needn't be tracked separately.
I guess I missed the original point of the thread.
My point was just that initonly works as documented.
Perhaps, but it functions differently than native C++ const or C# readonly,
and the behavior makes no sense. If an initonly reference is supposed to
point to an immutable object, then it should carry the initonly attribute on
the referred-to type and not be lost without a const_cast. It then would
look like this:
array<initonly bool>^ b;
But that makes no sense, because it raises the question "Which constructor
is allowed to change the value?"
If you have
array<bool>^ initonly b;
then it is only natural to be able to
b[0] = false;
Mark
--
Mark Salsbery
Microsoft MVP - Visual C++
"Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx> wrote in message
news:eoTVdpdMIHA.5244@xxxxxxxxxxxxxxxxxxxxxxx
"Ben Voigt [C++ MVP]" <rbv@xxxxxxxxxxxxx> wrote in message
news:%23wySAodMIHA.4712@xxxxxxxxxxxxxxxxxxxxxxx
"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; }
};
oops, missed the initialization of bprime, should have been
void doit() { /* b[0] = false; */ array<bool>^ bprime = b; bprime[0] =
false; }
.
- Follow-Ups:
- Re: initonly array member as an lvalue
- From: Mark Salsbery [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]
- Re: initonly array member as an lvalue
- From: Ben Voigt [C++ MVP]
- Re: initonly array member as an lvalue
- From: Ben Voigt [C++ MVP]
- Re: initonly array member as an lvalue
- From: Mark Salsbery [MVP]
- initonly array member as an lvalue
- Prev by Date: Re: Conditional compilation within template
- Next by Date: Re: Pro*C/C++ Visual Studio 2005
- Previous by thread: Re: initonly array member as an lvalue
- Next by thread: Re: initonly array member as an lvalue
- Index(es):
Relevant Pages
|