Advanced C++ | Conversion Operators
In C++, the programmer abstracts real world objects using classes as concrete types. Sometimes it is required to convert one concrete type to another concrete type or primitive type implicitly.
Conversion operators play smart role in such situations.
For example consider the following class
1 #include <iostream>
2 #include <cmath>
3 using namespace std;
4
5 class Complex
6 {
7 private:
8 double real;
9 double imag;
10
11 public:
12 // Default constructor
13 Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)
14 {
15
16 }
17
18 // magnitude : usual function style
19 double mag()
20 {
21 return getMag();
22 }
23
24 // magnitude : conversion operator
25 operator double ()
26 {
27 return getMag();
28 }
29
30 private:
31 // class helper to get magnitude
32 double getMag()
33 {
34 return sqrt(real * real + imag * imag);
35 }
36 };
37
38 int main()
39 {
40 // a Complex object
41 Complex com(3.0, 4.0);
42
43 // print magnitude
44 cout << com.mag() << endl;
45 // same can be done like this
46 cout << com << endl;
47 }
We are printing the magnitude of Complex object in two different ways.
Note that usage of such smart (over smart ?) techniques are discouraged. The compiler will have more control in calling an appropriate function based on type, rather than what the programmer expects. It will be good practice to use other techniques like class/object specific member function (or making use of C++ Variant class) to perform such conversions. At some places, for example in making compatible calls with existing C library, these are unavoidable.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
转载请注明:http://www.cnblogs.com/iloveyouforever/
2013-11-26 21:47:18
Advanced C++ | Conversion Operators的更多相关文章
- Conversion Operators in OpenCascade
Conversion Operators in OpenCascade eryar@163.com Abstract. C++ lets us redefine the meaning of the ...
- LINQ 学习路程 -- 查询操作 Conversion Operators
Method Description AsEnumerable Returns the input sequence as IEnumerable<t> AsQueryable Conve ...
- Bing Advanced Search Tricks You Should Know
Bing is one of the world's most popular search engines that has gained many fans with its ease of us ...
- Google C++ Style Guide
Background C++ is one of the main development languages used by many of Google's open-source project ...
- Data Types
原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...
- Google C++ 代码规范
Google C++ Style Guide Table of Contents Header Files Self-contained Headers The #define Guard For ...
- C++ Core Guidelines
C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...
- One SQL to Rule Them All – an Efficient and Syntactically Idiomatic Approach to Management of Streams and Tables(中英双语)
文章标题 One SQL to Rule Them All – an Efficient and Syntactically Idiomatic Approach to Management of S ...
- [c++] Operator overloading
c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...
随机推荐
- fork()和vfork()的区别,signal函数用法,exec()系列函数的用法小结
一:fork()和vfork()的区别: fork()函数可以创建子进程,有两个返回值,即调用一次返回两个值,一个是父进程调用fork()后的返回值,该返回值是刚刚创建的子进程的ID;另一个是子 ...
- Java测试开发--Maven用法(三)
一.Maven简介 Maven 是java项目构建工具,统一包的管理,统一项目管理.项目编译,测试打包.部署. 二.Maven工程搭建: 1.新建maven工程,如下图 2. 新建工程后,jdk使用的 ...
- loadRunner运行场景时,事务数为0或是只显示添加的事务的数
脚本编辑好后,不要着急到controller去执行,注意查看Run-time Settings(运行是设置)-->General(常规)-->Miscellaneous(其他)中查看Aut ...
- uni-app视频组件设置圆角
无法实现,建议写个image在中间位置加个播放按钮,点击播放跳转新页面只需要在跳转参数里面把视频链接加上,在onLoad里面获取视频链接,自动播放视频,很多app目前都是这样做的,关闭页面后视频会自动 ...
- 关于Thread的interrupt
关于Thread的interrupt Thread的interrupt方法会引发线程中断. 主要有以下几个作用: 如Object的wait方法,Thread的sleep等等这些能够抛出Interrup ...
- c++学习笔记(五)
数组作为函数参数 定义 数组可以作为函数的参数使用,进行数据传送. 数组用作函数参数有两种形式,一种是把数组元素(下标变量)作为实参使用:另一种是把数组名作为函数的形参和实参使用. 1.数组元素作为函 ...
- Typora图片自动上传至码云
Typora图片自动上传至码云 下载PicGo图片上传工具 PicGo下载地址 下载完毕后打开PicGo,点击插件设置,搜索Gitee,点击安装gitee 2.0.3 码云仓库创建 创建参数是点击设置 ...
- Python基础(slice切片)
l = ['傻狗1','傻狗2','傻狗3','傻狗4','傻狗5','傻狗6'] print(l[0:3])#['傻狗1', '傻狗2', '傻狗3'] numbers = list(range(1 ...
- Qt Creator 源码学习笔记01,初识QTC
阅读本文大概需要 4 分钟 Qt Creator 是一款开源的轻量级 IDE,整个架构代码全部使用 C++/Qt 开发而成,非常适合用来学习C++和Qt 知识,这也是我们更加深入学习Qt最好的方式,学 ...
- 什么是CLI、GUI
就是命令行界面command-line interface,也有人称之为字符用户界面(CUI). 通常认为,命令行界面(CLI)没有图形用户界面(GUI)那么方便用户操作. 因为,命令行界面的软件通常 ...