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 = ...
随机推荐
- 【数据结构&算法】10-串基础&KMP算法源码
目录 前言 串的定义 串的比较 串的抽象类型数据 串与线性表的比较 串的数据 串的存储结构 串的顺序存储结构 串的链式存储结构 朴素的模式匹配算法 模式匹配的定义 朴素的匹配方法(BRUTE FORC ...
- 学信网改绑手机号码,但是忘记了老号码怎么办?利用node.js + puppeteer 跑脚本实现改绑手机号
最近登录学信网发现自己学信网上绑定的手机号码不是目前自己使用的手机号码,于是想改绑手机号,但是发现不记得之前的手机号码了: 于是百度各种方法都无济于事:也不想重新注册账号,最后看见一篇文章通过Pyth ...
- sqlalchemy create single table
User.__table__.drop(engine) User.__table__.create(engine) https://stackoverflow.com/a/45287771/80250 ...
- 关于 RocketMQ 事务消息的正确打开方式 → 你学废了吗
开心一刻 昨晚和一哥们一起吃夜宵,点了几瓶啤酒 不一会天空下起了小雨,哥们突然道:糟了 我:怎么了 哥们:外面下雨了,我老婆还在等着我去接她 他给了自己一巴掌,说道:真他妈不是个东西 我心想:哥们真是 ...
- Numpy (嵩老师.)
import numpy as np Numpy 一元函数 对ndarray中的数据执行元素级运算的函数 np.abs(x) np.fabs(x) 计算数组各元素的绝对值 np.sqrt(x) 计算数 ...
- Dapr-发布/订阅
前言 前篇文章对Dapr的状态管理进行了解,本篇继续对 订阅/发布 构建块进行了解. 一.定义: 发布订阅的概念来自于事件驱动架构(EDA)的设计思想,这是一种让程序(应用.服务)之间解耦的主要方式, ...
- [loj3285]Circus
将奶牛的状态用序列$\{a_{1},a_{2},...,a_{m}\}$来描述,其中$a_{i}$表示第$i$头奶牛的位置(奶牛数量为$m$) 下面,先来考虑对于某个特定的$m$如何处理: 对于一条简 ...
- [spojSUBLEX]Lexicographical Substring Search
建立后缀自动机,对于同一个节点,出现次数是相同的(right的大小),同时满足单调性(长度越长出现次数越少),所以只需要考虑最长的串即可. PS:似乎也并不需要求依次后缀的max,不知道为什么-- 1 ...
- 获取客户端Mac地址
近期有个需求,需要获取客户端Mac地址作为白名单验证的依据.使用.net,B/S架构.先百度找了一些获取mac地址的方法, using System; using System.Collections ...
- Atcoder Grand Contest 001 F - Wide Swap(拓扑排序)
Atcoder 题面传送门 & 洛谷题面传送门 咦?鸽子 tzc 来补题解了?奇迹奇迹( 首先考虑什么样的排列可以得到.我们考虑 \(p\) 的逆排列 \(q\),那么每次操作的过程从逆排列的 ...