运算符重载语法说明
Operator overload syntax clarification
有人能解释一下以下语法差异如何改变运算符的工作方式吗?
T & operator()(type one, type two)
const T * operator()(type one, type two)
T & operator()(type one) const
const T & operator()(type one) const
假设它们都是成员,则所有成员都按值接受一个type
对象。这意味着,至少在语义上,主体操作符有自己的type
对象副本。operator()
语法表示实例是可调用的。在operator()
(例如(type a, type b)
(之后的是参数列表。
这一个使用两个type
和type
,并返回对T
的引用。无法在const
实例上使用。
T & operator()(type one, type two)
它可以被称为这样的东西:
MyFunctor x;
type a, b;
T& r = x(a,b); // take reference
T c = x(a,b); // make copy from reference. Assumes T has copy constructor
此版本需要两个type
,并返回一个指向const T
的指针。无法在const
实例上使用。不能调用T
的任何非常数方法。
const T * operator()(type one, type two)
示例:
MyFunctor x;
type a, b;
const T* p1 = x(a,b); // pointer to const
T* p2 = x(a,b); // Error! Must have const T* on LHS
这一个采用单个type
,并返回对T
的引用。可以在所有实例上使用,常量或非常量。根据返回的引用所指的内容,它可能会破坏const
的一致性,允许您通过const方法修改内部数据:
T & operator()(type one) const
最后一个方法与上面的方法相同,只是不能调用返回所指的任何非const
方法。
const T & operator()(type one) const
MyFunctor x;
type a;
const T& r = x(a); // take reference to const
T c = x(a); // make copy from reference. Assumes T has copy constructor
T& r = x(a); // Error! Cannot take reference to non-const!
相关文章:
- 1d 智能指针不适用于语法 (*)++
- 助记符和指向成员语法的指针
- 有人能分解一下这个c++模板的语法吗
- C++避免重复声明的语法是什么
- QMetaObject invokeMethod的基于函数指针的语法
- 这个语法std::class<>{}(arg1, arg2) 在C++中是什么意思?
- 当使用通配符和null指针调用函数时,对输出的说明
- 为什么包含windows.h会产生语法错误,从而阻止类的实例化?(C2146,C2065)
- 关于C++中具有多重继承"this"指针的说明
- 单独定义模板化嵌套类方法的正确语法
- 共享指针和具有自定义删除程序的唯一指针之间的语法差异背后的任何原因
- 错误 C2760:语法错误:映射迭代器上意外的标记"标识符",预期的";"
- 16 位到 10 位转换代码说明
- 为什么我会收到错误 C2143 语法错误:缺少"*"之前的';'?
- 赋值到类型和空大括号. 语法说明
- c++模板语法说明
- 模板语法说明
- C++矩形类关于语法的说明
- 语法说明
- 运算符重载语法说明