在一个作业中,我被告知要为某个类实现 bool operator()(const T&, const T&)
。重载 bool operator
意味着允许将对象隐式转换为 bool
。用两个参数重载它是什么意思?这些参数是如何在调用端传递的?这是如何使用的?
最佳答案
那不是“operator bool”,而是 operator()
,带有两个 T
参数,返回一个 bool
。换句话说,它是一个二元谓词。你可以像这样使用它:
struct Foo
{
bool operator()(const T&, const T&); // should probably be const
};
...
Foo f;
T t1, t2;
bool b = f(t1, t2);
关于c++ - 用两个参数重载 operator bool 是什么意思?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26218757/