在.cpp文件中使用实现声明C++/CX WinRT属性的语法是什么?

Skr*_*sli 8 windows-8 windows-runtime c++-cx

我有一个这样的课:

public ref class Test
{
public:
    property int MyProperty;
};
Run Code Online (Sandbox Code Playgroud)

这有效.现在我想将MyProperty的实现移动到CPP文件.我得到编译器错误,我执行此操作时已定义属性:

int Test::MyProperty::get() { return 0; }
Run Code Online (Sandbox Code Playgroud)

这个的正确语法是什么?

pet*_*snd 19

在标题中将声明更改为:

public ref class Test
{
public:
    property int MyProperty
    {
        int get();
        void set( int );
    }
private:
    int m_myProperty;
};
Run Code Online (Sandbox Code Playgroud)

然后,在cpp代码文件中写下你的定义:

int Test::MyProperty::get()
{
    return m_myProperty;
}
void Test::MyProperty::set( int i )
{
    m_myProperty = i;
}
Run Code Online (Sandbox Code Playgroud)

您看到错误的原因是您已声明了一个简单的属性,编译器会为您生成一个实现.但是,您也尝试明确提供实现.请参阅:http://msdn.microsoft.com/en-us/library/windows/apps/hh755807(v = vs.110).aspx

大多数在线示例仅直接在类定义中显示实现.