Visual C++ .NET/Generics/Generic Interface
Версия от 15:31, 26 мая 2010; (обсуждение)
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; }
};