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

标签 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 属性上做错了什么

最佳答案

对C++/CX了解不多,从报错信息猜测

Stack overflow

执行 X = value; 实际上是调用 X::set()X = value; ......你进入无限循环,从而导致堆栈溢出。

基于 the documentation ,您需要为 property X 定义一个后备存储变量。

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 Yproperty Z 执行相同的操作。

关于C++/CX 属性在集合上抛出错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20171294/

相关文章:

c++ - const int& value = 12 和 const int value = 12 之间的区别;

c++ - cpp- lower_bound 返回错误的迭代器

c# - 困难的设置和获取组合属性

Javascript对象属性数组,无法访问值

c# - 像 C# 中那样处理 PHP 属性(getter 和 setter)

c++ - 函数变量而不是指向函数的指针

c++ - 带有 Xaml 应用程序的 Directx 工具包

xaml - 为什么在 WXGA 模拟器中 SystemTray 有一个底部边框?

c# - 如何在不播放或存储的情况下获取 mp3 文件的持续时间

c++ - 使用condition_variable::notify_all通知多个线程