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 = ...
随机推荐
- upload-labs通关攻略(1-11关)
upload-labs通关攻略 upload-labs是练习文件上传很好的一个靶场,建议把upload-labs关卡全部练习一遍 1.下载安装 下载地址 链接:https://pan.baidu.co ...
- kafka的安装
kafka是基于java环境的,所以需要先安装java环境 centos:yum install java-11-openjdk ubuntu:apt install default-jdk 默安装默 ...
- QuantumTunnel:内网穿透服务设计
背景 最近工作中有公网访问内网服务的需求,便了解了内网穿透相关的知识.发现原理和实现都不复杂,遂产生了设计一个内网穿透的想法. 名字想好了,就叫QuantumTunnel,量子隧道,名字来源于量子纠缠 ...
- java随手记 面向对象
// 可以把两个类放在同一个文件中,但文件中只能有一个类是公共类,且公共类必须与文件同名,即xxx.java,源代码中的每个类编译成class文件 // java库中的类 // java.util.* ...
- css语法规范、选择器、字体、文本
css语法规范 使用 HTML 时需要遵从一定的规范,CSS 也是如此.要想熟练地使用 CSS 对网页进行修饰,首先需要了解CSS 样式规则. CSS 规则由两个主要的部分构成:选择器以及一条或多条声 ...
- 子查询之 exists 和 in
exists exists用于检查一个子查询是否至少会返回一行数据(即检测行的存在),返回值为boolean型,true或false 语法 exists subquery /* 参数: subquer ...
- Springboot 启动分析
启动类 Spring 启动类主要是 Annotation (@SpringBootApplication) 和 main 方法体中的 SpringApplication.run. 其中注解 @Spri ...
- python datetime 增减以月为单位的时间段
datetime.timedelta(days=10) # 可以 datetime.timedelta(months=1) # 不可以 替代: from datetime import date ...
- [cf1444D]Rectangular Polyline
由于两种线段要交替出现,有解的必要条件即为$h=v$(以下均记为$n$) 进一步的,再假设两种线段依次对应于向量$(a_{i},0)$和$(0,b_{i})$,根据题意要求向量长度为给定值且和为0,那 ...
- [noi1774]array
容易想到树套树,但数据范围太大,会超时 考虑平衡树的作用,就是将这个区间内的所有数排序,所以可以离线+归并来处理,预处理复杂度$o(n\log n)$,然后考虑维护:1.删除:2.询问 删除操作维护可 ...