C++/CLI 中的 IComparer class 可以有模板参数列表吗?
Can the IComparer class in C++/CLI have a template argument list?
使用 Visual Studio 2015 和这个简单的代码我收到消息:"class System::Collections::IComparer may not have a template argument list" 当试图定义 MarketOrder_SortMaxPriceToMinPrice class.
在 C# 中,我可以使用模板参数创建 class/method,但在 C++ 中,我只能使用通用 Object^ 句柄。
using namespace System;
using namespace System::Collections;
ref class MarketOrder : public IComparable<MarketOrder^> //Works fine
{
public:
virtual int CompareTo(MarketOrder^ other);
};
ref class MarketOrder_SortMaxPriceToMinPrice : IComparer<MarketOrder^> //Not allowed
{
public:
virtual int Compare(MarketOrder^ x, MarketOrder^ y);
};
这是 C++/CLI 限制还是我做错了什么?
以下代码适用于我:
ref class MarketOrder_SortMaxPriceToMinPrice : IComparer
{
public:
virtual int Compare(Object^ x, Object^ y);
};
有两个IComparer
类,一个generic and one non-generic。您需要引用命名空间 System::Collections::Generic
中的那个。
此外,建议子类化 Comparer<T>
,而不是直接实现接口。这样,您会自动获得非泛型重载。
使用 Visual Studio 2015 和这个简单的代码我收到消息:"class System::Collections::IComparer may not have a template argument list" 当试图定义 MarketOrder_SortMaxPriceToMinPrice class.
在 C# 中,我可以使用模板参数创建 class/method,但在 C++ 中,我只能使用通用 Object^ 句柄。
using namespace System;
using namespace System::Collections;
ref class MarketOrder : public IComparable<MarketOrder^> //Works fine
{
public:
virtual int CompareTo(MarketOrder^ other);
};
ref class MarketOrder_SortMaxPriceToMinPrice : IComparer<MarketOrder^> //Not allowed
{
public:
virtual int Compare(MarketOrder^ x, MarketOrder^ y);
};
这是 C++/CLI 限制还是我做错了什么?
以下代码适用于我:
ref class MarketOrder_SortMaxPriceToMinPrice : IComparer
{
public:
virtual int Compare(Object^ x, Object^ y);
};
有两个IComparer
类,一个generic and one non-generic。您需要引用命名空间 System::Collections::Generic
中的那个。
此外,建议子类化 Comparer<T>
,而不是直接实现接口。这样,您会自动获得非泛型重载。