c++ operator
这篇博文是以前很久写的,贴在我的早期一个blog中,今天google一下,发现还真有不少人转载,可惜并不注明出处。那时觉得operator比较好玩。C++有时它的确是个耐玩的东东。operator它有两种用法,一种是operator overloading(操作符重载),一种是operator casting(操作隐式转换)。 .operator overloading
C++可以通过operator 重载操作符,格式如下:类型T operator 操作符 (),如比重载+,如下所示 [cpp] view plaincopy
template<typename T> class A
{
public:
const T operator + (const T& rhs)
{
return this->m_ + rhs;
}
private:
T m_;
}; 又比如STL中的函数对象,重载(),这是C++中较推荐的写法,功能与函数指针类似,如下所示
template<typename T> struct A
{
T operator()(const T& lhs, const T& rhs){ return lhs-rhs;}
}; operator casting
C++可以通过operator 重载隐式转换,格式如下: operator 类型T (),如下所示
class A
{
public:
operator B* () { return this->b_;}
operator const B* () const {return this->b_;}
operator B& () { return *this->b_;}
operator const B& () const {return *this->b_;} private:
B* b_;
};
A a;
当if(a),编译时,其中它转换成if(a.operator B*()),其实也就是判断 if(a.b_)
c++ operator的更多相关文章
- The Safe Navigation Operator (&.) in Ruby
The most interesting addition to Ruby 2.3.0 is the Safe Navigation Operator(&.). A similar opera ...
- Blender 脚本之 Operator 初探
addon(插件)用来扩展 Blender 的功能,跟其他软件里的 plugin(插件)一样,去掉不会影响软件的运行.插件可以加到 Blender 的用户偏好设置目录里,或者就在你所编辑的.blend ...
- Sequence Project Showplan Operator 序列映射运算符
Sequence Project Showplan Operator 序列映射运算符 序列映射运算符会从一个已经排序的集合里通过不停添加集合里的列执行计算. 运算符根据一个或多个列的值把输入集合分为多 ...
- [c++] Operator overloading
c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...
- EC笔记:第二部分:11:在operator=中处理“自我赋值”
已经一年半没有写过博客了,最近发现学过的知识还是需要整理一下,为知笔记,要开始收费了以前写在为知笔记上笔记也会慢慢的转到博客里. 话不多说,进入正题. 考虑考虑以下场景: 当某个对象对自身赋值时,会出 ...
- Swift学习(一):自定义运算符 operator
自定义运算符仅能包含这些字符: / = - + * % < >!& | ^.~ 运算符位置: 前置运算符 prefix 中间运算符 infix 后置运算符 postfix 运算符其 ...
- Bash 4.4 中新增的 ${parameter@operator} 语法
Bash 4.4 中新增了一种 ${...} 语法,长这样:${parameter@operator}.根据不同的 operator,它展开后的值可能是 parameter 这个参数的值经过某种转换后 ...
- 为什么operator>>(istream&, string&)能够安全地读入长度未知的字符串?
一般而言,实现"读入用户输入的字符串",程序中自然不能对用户输入的长度有所限定.这在C++中很容易实现,而在C中确没那么容易. 这一疑问,我在刚学C++的时候也在脑中闪现过:不过很 ...
- tensorflow添加自定义的auc计算operator
tensorflow可以很方便的添加用户自定义的operator(如果不添加也可以采用sklearn的auc计算函数或者自己写一个 但是会在python执行,这里希望在graph中也就是c++端执行这 ...
- Flink – window operator
参考, http://wuchong.me/blog/2016/05/25/flink-internals-window-mechanism/ http://wuchong.me/blog/201 ...
随机推荐
- python内置数据类型-字典和列表的排序 python BIT sort——dict and list
python中字典按键或键值排序(我转!) 一.字典排序 在程序中使用字典进行数据信息统计时,由于字典是无序的所以打印字典时内容也是无序的.因此,为了使统计得到的结果更方便查看需要进行排序. Py ...
- python学习笔记整理——元组tuple
Python 文档学习笔记2 数据结构--元组和序列 元组 元组在输出时总是有括号的 元组输入时可能没有括号 元组是不可变的 通过分拆(参阅本节后面的内容)或索引访问(如果是namedtuples,甚 ...
- window php redis扩展下载地址
redis扩展下载 http://windows.php.net/downloads/pecl/snaps/redis/
- Beta--项目冲刺第七天
胜利在望-- 队伍:F4 成员:031302301 毕容甲 031302302 蔡逸轩 031302430 肖阳 031302418 黄彦宁 会议内容: 1.站立式会议照片: 2.项目燃尽图 3.冲刺 ...
- oracle主键自增
oracle主键自增 1建立数据表 create table Test_Increase( userid number(10) primary key, /*主键,自动增加*/ ...
- Java GC收集器配置说明
根据Java GC收集器具体分类,我们可以看出JVM根据需求不同提供了三种选择:串行收集器.并行收集器.并发收集器. 串行收集器只适用于小数据量的情况,我们主要了解一下并行收集器和并发收集器.默认情况 ...
- ubuntu 下mysql中文乱码问题解决方案
mysql> show variables like 'character%';+--------------------------+----------------------------+ ...
- 100114B
bfs #include<iostream> #include<queue> #include<cstring> #include<cstdio> us ...
- SQL查询记录是否在另一个表中存在
1.需求 create table ta(id int);create table tb(id int);insert into ta values(1);insert into ta values( ...
- leetcode 235. Lowest Common Ancestor of a Binary Search Tree
Given a binary search tree (BST), find the lowest common ancestor (LCA) of two given nodes in the BS ...