Visual C++ .NET/Generics/Generic Interface

Материал из .Net Framework эксперт
Перейти к: навигация, поиск

generic interface demo

 
#include "stdafx.h"
generic <typename T>
public interface class IGInterface
{
   property T InnerObject;
};


interface constraint

 
#include "stdafx.h"
interface class I
{
    void f();
};
generic <typename T> where T : I
ref class MyGenericClass
{
    T t;
    public:
       MyGenericClass(T t_in) : t(t_in)
       {
           t->f();
       }
};
ref class C : I
{
   public:
       virtual void f()
       {
       }
};
int main()
{
     MyGenericClass<C^>^ r = gcnew MyGenericClass<C^>(gcnew C());
}


interface with overloaded operator

 
#include "stdafx.h"
interface class IAddition
{
      static IAddition^ operator+(IAddition^, IAddition^);
};
generic <typename T> where T : IAddition
ref class G
{
   T add(T t1, T t2) { return t1 + t2; }
};