我知道值类没有默认构造函数,因为编译器将此类中的所有元素初始化为零。但是值类中的数组未初始化:
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); }
但必须有另一种解决方案。
有人可以告诉我如何解决这个问题吗?