Re: initonly array member as an lvalue

Tech-Archive recommends: Speed Up your PC by fixing your registry




"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; }






.



Relevant Pages

  • Re: Anders Hejlsberg comment on immutable objects
    ... >>explicit interface implementation exists is so an interface name can ... I wouldn't cast to an ... > variable as a sort of implicit reference, ... readonly const MyStruct myInstance. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Anders Hejlsberg comment on immutable objects
    ... >> all references are implicitly const is in fact immutable. ... >cast it away, and as I've said many times, everyone, me and you included, ... Implicit conversions are quite different. ... >> the vtable pointer from the instance to the reference. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Problem in type casting
    ... The output of the "getConstraint()" method is of type "const ... I can't explain your link error, but your cast is actually attempting to ... You need to cast to a reference to get the ...
    (microsoft.public.vc.mfc)
  • Re: Abstract class variables question
    ... "Specified cast is not valid". ... Not sure why that is since a string can be null. ... checking IsNull before assigning an instance of DataType, ... like as if you'd tried to cast an actual null reference to a value type. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: Anders Hejlsberg comment on immutable objects
    ... >explicit interface implementation exists is so an interface name can class ... Improper implicit conversions constitute a nasty ... >> With value types, the variable's value is the object, not a reference ... Consider the affects of our different views on a const ...
    (microsoft.public.dotnet.languages.csharp)