テンプレート ref クラス (C++/CX)

C++ テンプレートはメタデータに発行されないため、プログラム内でパブリック アクセシビリティおよび保護されたアクセシビリティを持つことはできません。 もちろん、プログラムで、標準 C++ テンプレートを内部的に使用できます。 さらに、パブリック ref クラスをテンプレートとして定義し、明示的に特化したテンプレート ref クラスをプライベート ref クラスのプライベート メンバーとして宣言できます。

ref クラス テンプレートの作成

次の例は、プライベート ref クラスをテンプレートとして宣言する方法だけでなく、標準 C++ テンプレートを宣言し、それら両方をパブリック ref クラスのメンバーとして宣言する方法も示します。 Windows ランタイム型 (この場合は Platform::String^) を使用して、標準 C++ テンプレートを特化できることに注意してください。

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 言語リファレンス
名前空間参照