这个运算符()语法的作用是什么
What does this operator() syntax do?
在这段代码中http://drdobbs.com/cpp/184403774:
template <class L, class R>
class MinResult {
L& lhs_;
R& rhs_;
public:
operator L&() { return lhs_ < rhs_ ? lhs_ : rhs_; } // <----
operator R&() { return lhs_ < rhs_ ? lhs_ : rhs_; } // <----
MinResult(L& lhs, R& rhs) : lhs_(lhs), rhs_(rhs) {}
};
上面的代码试图在箭头所指的行上做什么
我是C++的初学者,我知道我们可以通过定义operator()
来覆盖/定义它
但是,它不应该像这个那样定义吗
L& operator() { return lhs_ < rhs_ ? lhs_ : rhs_; }
我确信这是一些不同的语法,因为operator()
应该是一个单词。此外,不能用不同的返回类型定义其中两个。
您可以定义
operator type() const
作为允许强制转换为type
的运算符。例如
class date {
public:
operator time_t() const; // convert to time_t
};
operator()
有不同的用途,它允许将类用作"函数",而
相关文章:
- "using namespace std;"在C++的作用是什么?
- 此代码中的" "是什么,它的基本作用是什么?
- 在模板参数中使用 {} 在 type_trait{} 中时,其作用是什么<T>?
- <<、>>算子在循环中的作用是什么
- 在c++中,减号和加号对字符串的作用是什么
- 窄集(_cast)的作用是什么
- 在这种情况下,#define 的作用是什么?
- 遍历模板参数包,没有争论的专业化的作用是什么?
- 在 c++ 中,没有 **std::fixed** 的 **std::setprecision()** 的作用是什么?
- 访问多个阵列时 CPU 缓存的作用是什么?
- 以下表达式的作用是什么:x = (a1,a2,..,n);
- 私人成员的作用是什么?
- 这段代码的作用是什么?
- *.exp文件的作用是什么
- ifstream::open()真正的作用是什么
- 如何在矩阵中使用指针.这行代码的作用是什么:int r=**(++b);
- 这个函数的作用是什么?与夹紧值有关
- 这个运算符()语法的作用是什么
- std::函数的模板参数中“const”修饰符和引用的作用是什么
- 字符串流在C++编程中的作用是什么