[英]Overloading template class comparison operators
I am having a ton of trouble with overloading comparison operators in C++. 我在使用C ++重载比较运算符时遇到了很多麻烦。 I don't think I am doing it correctly, any help is appreciated.
我认为我做的不正确,不胜感激。 I have provided a snippet of my class below.
我在下面提供了我班的摘录。
template <typename T>
class A
{
public:
template <typename T>
bool operator< (const A<T>& rhs) const {
return (value < rhs);
}
private:
T value;
};
If you want both A
objects to be parameterized with the same type T
, remove the inner template <typename T>
on the operator. 如果要用相同的类型
T
对两个A
对象进行参数化,请在运算符上删除内部template <typename T>
。
Otherwise, if you want the operator to take an A
with a different type parameter than T
, change the inner template <typename T>
to template <typename U>
, and change rhs
to const A<U>& rhs
. 否则,如果要让操作员采用类型参数与
T
不同的A
,请将内部template <typename T>
更改为template <typename U>
,并将rhs
更改为const A<U>& rhs
。
If your operator<
accepts an A
object whose template parameter is a different type then the A
object being compared to, you have to use different template parameter names: 如果您的
operator<
接受模板参数为不同类型的A
对象,然后接受与之比较的A
对象,则必须使用不同的模板参数名称:
template <typename T>
class A
{
public:
template <typename Other>
bool operator< (const A<Other>& rhs) const {
return (value < rhs.value);
}
private:
T value;
};
This will work as long as T
and Other
are types that are comparable with <
. 只要
T
和Other
是与<
可以比较的类型,它将起作用。
However, if you are comparing two A
objects that have the same template type, there is no need for operator<
to have its own template at all: 但是,如果要比较具有相同模板类型的两个
A
对象,则operator<
根本不需要拥有自己的模板:
template <typename T>
class A
{
public:
bool operator< (const A<T>& rhs) const {
return (value < rhs.value);
}
private:
T value;
};
If you want to support simple use cases, such as: 如果要支持简单的用例,例如:
A<int> a = { some_value };
A<int> b = { some_other_value } ;
if ( a < b ) { ... }
there is no need for the operator<
function to be member function template. 无需将
operator<
函数作为成员函数模板。 You can simplify your code to: 您可以将代码简化为:
template <typename T>
class A
{
public:
bool operator<(const A& rhs) const {
return (value < rhs.value);
}
private:
T value;
};
If you want to be able to use: 如果您希望能够使用:
A<int> a = { some_value };
A<float> b = { some_other_value } ;
if ( a < b ) { ... }
then the operator<
function needs to be a member function template. 那么
operator<
函数必须是成员函数模板。 However, make sure that the two template parameter names are different. 但是,请确保两个模板参数名称不同。 You can use:
您可以使用:
template <typename T>
class A
{
public:
template <typename T1>
bool operator<(const A<T1>& rhs) const {
return (value < rhs.value);
}
private:
T value;
};
That will work as long as T
and T1
support such an operation. 只要
T
和T1
支持这样的操作,它将起作用。
I am not sure if you want two different template parameters, or one. 我不确定是否要使用两个或两个不同的模板参数。 This is the solution with only one typename (I also added getter for value, just because I prefer to).
这是只有一个类型名的解决方案(我也为自己的值添加了getter,只是因为我更喜欢)。 You don't need inside definition of template and you need to compare the values of the instances, not just value and instance A.
您不需要模板的内部定义,并且您需要比较实例的值,而不仅仅是值和实例A。
template <typename T>
class A {
public:
bool operator < (const A<T> & rhs) const {
return (value < rhs.getValue());
}
const T & getValue() const{
return this -> value;
}
private:
T value;
};
If you want two template arguments there, that is more complicated. 如果在那里需要两个模板参数,那就更复杂了。 Unless you have just few classes that need to work like this, you can do < operator for each.
除非只有少数几个需要像这样工作的类,否则您可以为每个类做<运算符。 Otherwise it could get messy and I am sure there would be nicer solution for what you want to do.
否则,它可能会变得凌乱,我相信您想要做的事情会有更好的解决方案。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.