为什么属性不能在ref类密封的情况下公开 [英] Why can properties not be public inside a ref class sealed
问题描述
以下代码是非法的(Visual Studio 2012 Windows Phone(创建Windows Phone Direct3d应用程序))
The following code is illegal ( Visual Studio 2012 Windows Phone( Creating a windows phone direct3d app ) )
a non-value type cannot have any public data members 'posX'
标题
ref class Placement sealed
{
public:
Placement(
float rotX,
float rotY,
float rotZ,
float posX,
float posY,
float posZ
);
float rotX, rotY, rotZ, posX, posY, posZ;
};
Cpp
Placement::Placement(
float rotX,
float rotY,
float rotZ,
float posX,
float posY,
float posZ
)
: posX( posX ),
posY( posY ),
posZ( posZ )
{
this->rotX = static_cast<float>(rotX);
this->rotY = static_cast<float>(rotY);
this->rotZ = static_cast<float>(rotZ);
}
为什么以及如何设置属性?我已经习惯了普通的C ++,而不是C ++ CX(我认为这是正确的吗?)...我是否必须创建用于服务属性的方法?
*这个问题源于我本来试图创建一个普通类并创建一个指向它的指针,但只能抱怨我不能使用*,而不得不使用^,这意味着我必须创建一个ref类反而... 我真的不明白为什么?*
*This problem stemmed from me originally trying to create a normal class and creating a pointer to it, only to be complained to about the fact I cannot use * rather I have to use ^ which means I have to create a ref class instead... I don't really understand why?*
与WinRT或更具体地与ARM处理器有关吗?
推荐答案
这是WinRT和C ++/CX扩展所特有的. C ++/CX不允许引用类包含公共字段.您需要用公共属性替换公共字段.
This is something specific to WinRT and the C++/CX extensions specifically. C++/CX does not allow ref classes to contain public fields. You need to replace your public fields with public properties.
ref class Placement sealed
{
public:
Placement(
float rotX,
float rotY,
float rotZ,
float posX,
float posY,
float posZ
);
property float rotX;
property float rotY;
property float rotZ;
property float posX;
property float posY;
property float posZ;
};
属性具有由编译器自动生成的getter和setter函数.
Properties have getter and setter functions generated automatically for them by the compiler.
这篇关于为什么属性不能在ref类密封的情况下公开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!