August 22, 2011

Properties in C++/CLI

See also C++/CLI lesson here
To implement them completely inline:
virtual property bool IsWritable {
  bool get() { return isWritable_; };
  void set(bool value) { isWritable_ = value; };
}
In header file (.h) for a header+source implementation:
virtual property System::String ^Name {
  System::String ^get();
  void set(System::String^); 
}
then in the source (.cpp) file:
System::String ^ XXX::Name::get() {
  return gcnew System::String(xxx.getName().c_str());
}

void XXX::Name::set(System::String ^value) {
   ...
}
To override, in the header of derived class change the definition with an override statement:
virtual property bool DoSomething {
  bool get() override;
  bool set() override;
}

No comments: