initonly数组成员作为左值 [英] initonly array member as an lvalue
问题描述
在C#中,一个标有''readonly''的数组,如:
public readonly bool [] array = new bool [8];
将允许修改数组的各个成员,而
保护数组引用本身不被更改。也就是说,我
可以做到这一点:
array [0] = false;
但不是这个:
array = null;
但是,看起来C ++ / CLI中的initonly关键字看起来不像
完全一样。如果我做这样的事情:
公共参考课这件事
{
公开:
Thing();
void SetBool();
protected:
initonly array< bool> ^ bArr;
};
Thing :: Thing()
{
bArr = gcnew array< bool>(8); < br $>
}
void Thing :: SetBool()
{
bArr [0] = false ;
}
我收到有关使用''initonly''字段作为l-
值的编译错误。什么是正确的方法?
In C#, an array marked ''readonly'', as in:
public readonly bool[] array = new bool[8];
would allow individual members of the array to be modified while
protecting the array reference itself from being changed. That is, I
could do this:
array[0] = false;
but not this:
array = null;
However, it doesn''t seem like the ''initonly'' keyword in C++/CLI does
exactly the same thing. If I do something like this:
public ref class Thing
{
public:
Thing();
void SetBool();
protected:
initonly array<bool>^ bArr;
};
Thing::Thing()
{
bArr = gcnew array<bool>(8);
}
void Thing::SetBool()
{
bArr[0] = false;
}
I get a compilation error about using an ''initonly'' field as an l-
value. What''s the right way to go about this?
推荐答案
您的初始化需要在构造函数中完成,即使是
阵列。
Mark
-
Mark Salsbery
Microsoft MVP - Visual C ++
" lee.crabtree" < le ********** @ gmail.comwrote in message
news:c0 ******************** ************** @ s6g2000p rc.googlegroups.com ...
Your initialization needs to be done in the constructor, even for items in
the array.
Mark
--
Mark Salsbery
Microsoft MVP - Visual C++
"lee.crabtree" <le**********@gmail.comwrote in message
news:c0**********************************@s6g2000p rc.googlegroups.com...
在C#中,一个标有''readonly'的数组',如:
public readonly bool [] array = new bool [8];
将允许数组的各个成员
时修改,保护数组引用本身不被更改。也就是说,我
可以做到这一点:
array [0] = false;
但不是这个:
array = null;
但是,看起来C ++ / CLI中的initonly关键字看起来不像
完全一样。如果我做这样的事情:
公共参考课这件事
{
公开:
Thing();
void SetBool();
protected:
initonly array< bool> ^ bArr;
};
Thing :: Thing()
{
bArr = gcnew array< bool>(8); < br $>
}
void Thing :: SetBool()
{
bArr [0] = false ;
}
我收到有关使用''initonly''字段作为l-
值的编译错误。什么是正确的方法?
In C#, an array marked ''readonly'', as in:
public readonly bool[] array = new bool[8];
would allow individual members of the array to be modified while
protecting the array reference itself from being changed. That is, I
could do this:
array[0] = false;
but not this:
array = null;
However, it doesn''t seem like the ''initonly'' keyword in C++/CLI does
exactly the same thing. If I do something like this:
public ref class Thing
{
public:
Thing();
void SetBool();
protected:
initonly array<bool>^ bArr;
};
Thing::Thing()
{
bArr = gcnew array<bool>(8);
}
void Thing::SetBool()
{
bArr[0] = false;
}
I get a compilation error about using an ''initonly'' field as an l-
value. What''s the right way to go about this?
即使是一个值类型数组?这看起来很奇怪。那还是
并不能解释为什么数组中的项目不能用作l-
的价值。
11月26日下午2:50,Mark Salsbery [MVP]
< MarkSalsbery [MVP] @ newsgroup.nospamwrote:
Even for an array of value types? That seems strange. That still
doesn''t explain why the items in the array can''t be used as an l-
value.
On Nov 26, 2:50 pm, "Mark Salsbery [MVP]"
<MarkSalsbery[MVP]@newsgroup.nospamwrote:
你的初始化需要在构造函数中完成,即使对于数组中的项目也是如此。
Mark
-
Mark Salsbery
Microsoft MVP - Visual C ++
Your initialization needs to be done in the constructor, even for items in
the array.
Mark
--
Mark Salsbery
Microsoft MVP - Visual C++
这是VS2008上的错误:
"错误C3893:''Thing :: bArr'':l值使用initonly数据成员只是
允许在类''Thing''的实例构造函数中使用"
l-value指的是使用变量作为赋值中的左值 -
这不允许在构造函数之外用于initonly变量。
Mark
-
Mark Salsbery
Microsoft MVP - Visual C ++
" lee.crabtree" < le ********** @ gmail.comwrote in message
news:30 ******************** ************** @ v4g2000h sf.googlegroups.com ...
Here''s the error on VS2008:
"error C3893: ''Thing::bArr'' : l-value use of initonly data member is only
allowed in an instance constructor of class ''Thing''"
l-value refers to using the variable as the left-value in an assignment -
this is not allowed outside the constructor for initonly variables.
Mark
--
Mark Salsbery
Microsoft MVP - Visual C++
"lee.crabtree" <le**********@gmail.comwrote in message
news:30**********************************@v4g2000h sf.googlegroups.com...
即使是一组值类型?这看起来很奇怪。那还是
并不能解释为什么数组中的项目不能用作l-
的价值。
11月26日下午2:50,Mark Salsbery [MVP]
< MarkSalsbery [MVP] @ newsgroup.nospamwrote:
Even for an array of value types? That seems strange. That still
doesn''t explain why the items in the array can''t be used as an l-
value.
On Nov 26, 2:50 pm, "Mark Salsbery [MVP]"
<MarkSalsbery[MVP]@newsgroup.nospamwrote:
>你的初始化需要在构造函数中完成,即使对于数组中的项目
也是如此。
Mark
-
Mark Salsbery
Microsoft MVP - Visual C ++
>Your initialization needs to be done in the constructor, even for items
in
the array.
Mark
--
Mark Salsbery
Microsoft MVP - Visual C++
这篇关于initonly数组成员作为左值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!