集合上的C ++ / CX属性抛出错误

时间:2013-11-24 05:00:26

标签: c++ properties windows-phone-8 c++-cx

我有一个班级:

ref class Coord
{
public:
    property float X {
        float get() { return X; }
        void set( float value ) 
            { 
                X = value; // THROWS EXCEPTION
            }
    };
    property float Y {
        float get() { return Y; }
        void set( float value ) { Y = value; }
    };
    property float Z {
        float get() { return Z; }
        void set( float value ) { Z = value; }
    };
};

我制作了一份新副本:

Coord^ playerRotation = ref new Coord();

我尝试设置其中一个属性的值:

playerRotation->X = 0.0f;

它运行到我的类代码的这一部分:

X = value; // THROWS EXCEPTION

并抛出异常:

Unhandled exception at 0x00115299 in Game.exe: 0xC00000FD: Stack overflow (parameters: 0x00000001, 0x008E2FD0).

我在使用C ++ / CX属性时遇到了什么错误

1 个答案:

答案 0 :(得分:2)

我对C ++ / CX了解不多,但是从错误消息中猜测

  

堆栈溢出

执行X = value;实际上正在调用X::set(),而X = value;依次执行property X ...您将进入无限循环,从而导致堆栈溢出。

基于the documentation,您需要为ref class Coord { float m_x; // Backing store for property X public: property float X { float get() { return m_x; } void set( float value ) { m_x = value; } }; ... 定义后备存储变量。

property Y

您可能还想对property Z和{{1}}执行相同操作。