我知道值类没有默认构造函数,因为编译器用零初始化此类中的所有元素。但是数组在值类不被初始化:值类中的CLI数组初始化
value class c_LocationVal
{
public:
double x, y, z;
c_LocationVal(double i_x, double i_y, double i_z) {x = i_x; y = i_y; z = i_z;}
};
typedef cli::array<c_LocationVal> arrloc;
value class c_Managed
{
public:
arrloc^ m_alocTest;
//c_Managed() { m_alocTest = gcnew arrloc(3); } --> not permitted
double funcManaged()
{
return m_alocTest[0].x; --> error: Object reference not set to an instance of an object
}
};
我能欺骗和利用:
c_Managed(int i) { m_alocTest = gcnew arrloc(3); }
但必须有另一种解决方案。
有人可以告诉我如何解决这个问题吗?
C#中实际需要你称作弊。我假设C++/CLI的规则是一样的。难道你不能声明'c_LocationVal'的C数组吗? – leppie
不幸的是,我不能:当我创建一个托管类型的C数组时,我将得到编译器错误C2728,并且当我在托管类型内创建一个非托管类型的数组时,我将得到编译器错误C4368。而且因为主应用程序是一个CLR-Winform,我不能使用非托管数组。 –
[我如何在C++/CLI中指定一个固定大小的缓冲区?](http://stackoverflow.com/questions/2138473/how-do-i-specify-a-fixed-size-buffer-in -c-cli) –