Visual C++ .NET/Generics/Generic Interface

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

generic interface demo

<source lang="csharp">

  1. include "stdafx.h"

generic <typename T> public interface class IGInterface {

  property T InnerObject;

};

 </source>


interface constraint

<source lang="csharp">

  1. 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());

}

 </source>


interface with overloaded operator

<source lang="csharp">

  1. 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; }

};

 </source>