템플릿 ref 클래스(C++/CX)

C++ 템플릿은 메타데이터에 게시되지 않으므로 프로그램에서 public 또는 protected 액세스 가능성을 가질 수 없습니다. 물론 표준 C++ 템플릿을 프로그램에서 내부적으로 사용할 수 있습니다. 또한 private ref 클래스를 템플릿으로 정의하고 명시적으로 특수화된 템플릿 ref 클래스를 public ref 클래스에서 private 멤버로 선언할 수 있습니다.

ref 클래스 템플릿 작성

다음 예제에서는 private ref 클래스를 템플릿으로 선언하는 방법 및 표준 C++ 템플릿을 선언하는 방법과 이 둘을 public ref 클래스에서 멤버로 선언하는 방법을 보여 줍니다. 표준 C++ 템플릿은 Windows 런타임 형식(이 경우 Platform::String^)으로 특수화할 수 있습니다.

namespace TemplateDemo
{
    // A private ref class template
    template <typename T>
    ref class MyRefTemplate
    {
    internal:
        MyRefTemplate(T d) : data(d){}
    public:
        T Get(){ return data; }
    private:
        T data;
    };

    // Specialization of ref class template
    template<>
    ref class MyRefTemplate<Platform::String^>
    {
    internal:
        //...
    };

    // A private derived ref class that inherits
    // from a ref class template specialization
    ref class MyDerivedSpecialized sealed : public MyRefTemplate<int>
    {
    internal:
        MyDerivedSpecialized() : MyRefTemplate<int>(5){}
    };

    // A private derived template ref class 
    // that inherits from a ref class template
    template <typename T>
    ref class MyDerived : public MyRefTemplate<T>
    {
    internal:
        MyDerived(){}
    };

    // A standard C++ template
    template <typename T>
    class MyStandardTemplate
    {
    public:
        MyStandardTemplate(){}
        T Get() { return data; }
    private:
        T data;

    };

    // A public ref class with private 
    // members that are specializations of
    // ref class templates and standard C++ templates.
    public ref class MySpecializeBoth sealed
    {
    public:
        MySpecializeBoth(){}
    private:
        MyDerivedSpecialized^ g;
        MyStandardTemplate<Platform::String^>* n;
    };
}

참고 항목

형식 시스템(C++/CX)
C++/CX 언어 참조
네임스페이스 참조