I've been working on a managed C++ application that uses the C++/CLI (Whidbey/2005/etc) syntax. While many of the new syntax changes have been really useful, I found that there is practically no documentation on how to split properties between header (.h) and implementation (.cpp) files. I figured I'd post this for any folks who come across the same issue.
Let's say you have this .h file:
ref class MyClass{public: property int MyProperty;};
By default, the C++ compiler will automatically back MyProperty with a hidden int (or whatever you're using). This means that you don't need to give it any implementation code in your .cpp. In fact, if you try to, it'll throw an error that you've already defined its get/set since this is implicitly done. If you want to define your own implementation for the get/set, you'll need to change it to look like this:
ref class MyClass{public: property int MyProperty { int get(); void set(int value); }};
At the same time, you're now on the hook for fulfilling these methods yourself, so your .cpp file will need to look something like this:
int MyClass::MyProperty::get(){ // Do what you need to return the property value}void MyClass::MyProperty::set(int value){ // Do what you need to set the property value}
Note that you don't need to specify any special keywords or format as get_ or set_ and you can use any valid name for the set() parameter ("value" isn't required). If you want to make your property read-only, then you need to explicitly provide a get() but not a set(). If you want to make your property write-only, put the keyboard down and take three steps backwards