【足迹C++primer】49、超载,变化,运营商
2015-06-15 12:20
218 查看
超载,变化,运营商
Conversion Operators
operator type() const
Conversions to
an array or a function type are not permitted.
转换函数必须是成员函数。不能指定返回 类型,必须有一个空的參数列表。
函数通常应 const。
Defining a Class with a Conversion Operator(献给热爱英语的朋友
定义一个类,表示一个在0到255范围类的一个整数
实现下面:
Conversion Operators Can Yield Suprising Results
说是这样说可是究竟有什么惊人的效果,反正我是没感觉!
explicit Conversion Operators
说白了就是叫我们使用explicit来使用!
我来说一下。人活在这世界上肯定会遭遇非常多不幸和痛苦,并且一般这样的不幸的事从来都不是一个一个来的,他们会在你措手不及的时候一下子全来了,就像精明的猎人。在你露出破绽的一瞬间把所有的活力倾泻在你的身上,确保万无一失。
每一个人不可能一辈子都不犯错,可是当厄运来临的时候我们要时刻准备好,不要惊慌失措,冷静下来,好好想一想你应该怎样去面对!
Conversion Operators
转换操作符
operator type() constConversions to
an array or a function type are not permitted.
转换函数必须是成员函数。不能指定返回 类型,必须有一个空的參数列表。
函数通常应 const。
Defining a Class with a Conversion Operator(献给热爱英语的朋友
)
定义一个类,表示一个在0到255范围类的一个整数class SmallInt { public: SmallInt(int i=0):val(i) { if(i<0 || i>255) throw std::out_of_range("越界! !。越界进球无效!"); } explicit operator int() const {return val;} //转换操作符,不能有參数。是成员函数,没有返回类型 private: size_t val; };
实现下面:
void fun1() { SmallInt si; si=4; //吧4隐式转换成SmallInt类型 // si+3; //隐式转换si为int类型,因为explicit这里会出错,加了explicit之后仅仅能显式转换 }
Conversion Operators Can Yield Suprising Results
重载操作符能产生惊人的结果
void fun2() { int i=42; // cin<<i; this code would be legal if the conversion to bool were not explicit! }
说是这样说可是究竟有什么惊人的效果,反正我是没感觉!
explicit Conversion Operators
明白转换操作
说白了就是叫我们使用explicit来使用!全代码!这次就简洁点哈哈。
/**
* 功能:重载。转换,运算符
* 时间:2014年7月19日15:05:21
* 作者:cutter_point
*/
#include<iostream>
#include <stdexcept>
using namespace std;
/**************************************
Conversion Operators
转换操作符
**************************************/
/*
operator type() const
Conversions to
an array or a function type are not permitted.
转换函数必须是成员函数。不能指定返回 类型。必须有一个空的參数列表。
函数通常应 const。
*/
/**
Defining a Class with a Conversion Operator
*/
//定义一个类,表示一个在0到255范围类的一个整数
class SmallInt
{
public:
SmallInt(int i=0):val(i)
{
if(i<0 || i>255)
throw std::out_of_range("越界!!!越界进球无效!
");
}
explicit operator int() const {return val;} //转换操作符,不能有參数。是成员函数。没有返回类型
private:
size_t val;
};
void fun1()
{
SmallInt si;
si=4; //吧4隐式转换成SmallInt类型
// si+3; //隐式转换si为int类型,因为explicit这里会出错。加了explicit之后仅仅能显式转换
}
/**
Conversion Operators Can Yield Suprising Results
重载操作符能产生惊人的结果
*/
void fun2() { int i=42; // cin<<i; this code would be legal if the conversion to bool were not explicit! }
/**
explicit Conversion Operators
明白转换操作
*/
//说白了就是叫我们使用explicit来使用!
int main()
{
return 0;
}
我来说一下。人活在这世界上肯定会遭遇非常多不幸和痛苦,并且一般这样的不幸的事从来都不是一个一个来的,他们会在你措手不及的时候一下子全来了,就像精明的猎人。在你露出破绽的一瞬间把所有的活力倾泻在你的身上,确保万无一失。
每一个人不可能一辈子都不犯错,可是当厄运来临的时候我们要时刻准备好,不要惊慌失措,冷静下来,好好想一想你应该怎样去面对!
相关文章推荐
- C++中的类型转换static_cast、dynamic_cast、const_cast和reinterpret_cast
- dynamic_cast.cpp
- operator.cpp
- friend_class.cpp
- c语言
- stdarg.cpp
- C++文件读写略解(fstream)
- C++ 自定义事件机制
- 面向对象程序设计-C++ Finial exam review NOTES【第十六次上课笔记】
- c++空类实例大小不是0原因
- C++10.5 multimap和multiset类型---equal_range、lower_bound 和upper_bound函数
- Ubuntu 12.04 配置c++开发环境
- Effective C++ 条款49
- C++中string <-> int
- 找工作笔试面试那些事儿(8)---常问的CC++基础题
- 找工作笔试面试那些事儿(4)---C++函数高级特征
- c++_基本数据类型_Size
- C++【子类的构造和析构】
- [C/C++]_[初级]_[malloc-calloc-new的区别]
- [C/C++]_[初级]_[malloc-calloc-new的区别]