Ref classes (Visual C++ for Windows Runtime)

Expand
7 out of 8 rated this helpful - Rate this topic

Ref classes (Visual C++ for Windows Runtime)

[This documentation is for preview only, and is subject to change in later releases. Blank topics are included as placeholders.]

A ref class is a user-defined class that can be passed across the ABI boundary.

The following examples shows how to declare a ref class:

ref class Person
{
public:
  typedef Windows::Foundation::Collections::IMapView<Platform::String^, Platform::String^> NumbersView;
  Person(Platform::String^ name);
  void AddPhoneNumber(Platform::String^ type, Platform::String^ number);
  property NumbersView^ PhoneNumbers
  { 
    NumbersView^ get();
  }
private:
  Platform::String^ name_;
  std::map<Platform::String^, Platform::String^> numbers_;
};

Note the use of std::map in the private members and IMapView in the public interface. This shows the general idiom in which you use standard C++ types internally and Windows Runtime types at the ABI boundaries.

The following example shows how to implement a ref class:

Person::Person(Platform::String^ name): name_(name) { }
void Person::AddPhoneNumber(Platform::String^ type, Platform::String^ number)
{
numbers_[type] = number;
}
Person::NumbersView^ Person::PhoneNumbers::get()
{
return ref new Platform::MapView<Platform::String^, Platform::String^>(numbers_);
}

The following example shows how client code uses the ref class:

Person^ p = ref new Person("Clark Kent");
p->AddPhoneNumber("Home", "425-555-4567");
p->AddPhoneNumber("Work", "206-555-9999");
Platform::String^ workphone = p->PhoneNumbers->Lookup("Work");

Did you find this helpful?
(1500 characters remaining)