What is the syntax for declaring a C ++ / CX WinRT property with an implementation in a .cpp file?

I have a class like this:

public ref class Test { public: property int MyProperty; }; 

It works. Now I want to move the implementation of MyProperty to a CPP file. I get compiler errors that the property is already defined when I do this:

 int Test::MyProperty::get() { return 0; } 

What is the correct syntax for this?

+8
windows-8 windows-runtime c ++ - cx
source share
2 answers

In the header, change the declaration to:

 public ref class Test { public: property int MyProperty { int get(); void set( int ); } private: int m_myProperty; }; 

Then, in your cpp code file, write your definitions as follows:

 int Test::MyProperty::get() { return m_myProperty; } void Test::MyProperty::set( int i ) { m_myProperty = i; } 

The reason you see errors is because you declared a trivial property in which the compiler creates an implementation for you. But then you also tried to implement the implementation. See: http://msdn.microsoft.com/en-us/library/windows/apps/hh755807(v=vs.110).aspx

In most online examples, only implementations are displayed directly in the class definition.

+18
source share

In the class definition, you need to declare the property as a property with user-declared get and set methods; this cannot be a shorthand property:

 public ref class Test { public: property int MyProperty { int get(); void set(int); } }; 

Then in the cpp file you can define the get() and set() methods:

 int Test::MyProperty::get() { return 42; } void Test::MyProperty::set(int) { // set the value } 
+4
source share

All Articles