Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the syntax to declare a C++/CX WinRT property with implementation in the .cpp file?

I have a class like this:

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

This works. Now I want to move the implementation of MyProperty to the 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 proper syntax for this?

like image 217
Skrymsli Avatar asked May 07 '12 16:05

Skrymsli


1 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 the cpp code file write your definitions like this:

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

The reason you are seeing errors is that you have declared a trivial property where the compiler generates an implementation for you. But, then you tried to provide an implementation explicitly as well. See: http://msdn.microsoft.com/en-us/library/windows/apps/hh755807(v=vs.110).aspx

Most of the examples online only show implementations directly in the class definition.

like image 60
petrsnd Avatar answered Jan 04 '23 10:01

petrsnd