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的更多相关文章

  1. Conversion Operators in OpenCascade

    Conversion Operators in OpenCascade eryar@163.com Abstract. C++ lets us redefine the meaning of the ...

  2. LINQ 学习路程 -- 查询操作 Conversion Operators

    Method Description AsEnumerable Returns the input sequence as IEnumerable<t> AsQueryable Conve ...

  3. 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 ...

  4. Google C++ Style Guide

    Background C++ is one of the main development languages used by many of Google's open-source project ...

  5. Data Types

    原地址: Home / Database / Oracle Database Online Documentation 11g Release 2 (11.2) / Database Administ ...

  6. Google C++ 代码规范

    Google C++ Style Guide   Table of Contents Header Files Self-contained Headers The #define Guard For ...

  7. C++ Core Guidelines

    C++ Core Guidelines September 9, 2015 Editors: Bjarne Stroustrup Herb Sutter This document is a very ...

  8. 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 ...

  9. [c++] Operator overloading

    c++的操蛋属性:自己为一档,空一档,其他随意. UB_stack a; UB_stack b = a; // copy auto c = a; auto d {a}; // (or auto d = ...

随机推荐

  1. CSS学习(二)选择符

    元素选择符:以元素名作为选择符(span{ color: red; }) 群组选择符:将两个选择符用逗号隔开构成群组(span, div{ color: red; }) 通用选择符:通用选择符(*)将 ...

  2. 攻防世界 WEB 高手进阶区 XCTF Web_python_template_injection Writeup

    攻防世界 WEB 高手进阶区 XCTF Web_python_template_injection Writeup 题目介绍 题目考点 SSTI模板注入漏洞 Writeup 知识补充 模板注入:模板引 ...

  3. Windows下git多用户配置

    refer from :https://blog.csdn.net/qq_39892503/article/details/109374201 windows git多用户配置 在安装git结束,我们 ...

  4. 用js实现web端录屏

    用js实现web端录屏 原创2021-11-14 09:30·无意义的路过 随着互联网技术飞速发展,网页录屏技术已趋于成熟.例如可将录屏技术运用到在线考试中,实现远程监考.屏幕共享以及录屏等:而在我们 ...

  5. Pycharm下载安装详细教程

    目录 1.Pycharm 简介 2.Pycharm下载 3.环境变量的配置 4.Pycharm的使用 1.Pycharm 简介 PyCharm是一种Python IDE(Integrated Deve ...

  6. react之redux状态管理

    1.传统MVC框架的缺陷 模型(model)-视图(view)-控制器(controller)的缩写 V即View视图:用户看到并与之交互的界面. M即Model模型是管理数据:很多业务逻辑都在模型中 ...

  7. C#-WPF数据绑定基础(一)

    前言:WPF数据绑定技术有效的提高了程序的容错率,可以最大程度的保持程序的健壮性,从而降低程序在使用过程中崩掉的可能性. 接下来,我将分享一下我在写测量程序过程中所用到的数据绑定方面的知识 首先,我所 ...

  8. [cf643G]Choosing Ads

    首先对于$p>50$,有经典的做法,即不断删去区间中不同的两数,最终剩下的即为出现次数超过一半的数(或没有),用线段树维护即可 那么对于$p\le 50$,类似的,即删去区间中不同的$\lflo ...

  9. 8.5 Ingress实现基于域名的多虚拟主机、URL转发、及多域名https实现等案例

    1.什么是Ingress Ingress 公开了从k8s集群外部到集群内服务的 HTTP 和 HTTPS 路由. 流量路由由 Ingress 资源上定义的规则控制. 可以将 Ingress 配置为服务 ...

  10. Redis、Zookeeper实现分布式锁——原理与实践

    Redis与分布式锁的问题已经是老生常谈了,本文尝试总结一些Redis.Zookeeper实现分布式锁的常用方案,并提供一些比较好的实践思路(基于Java).不足之处,欢迎探讨. Redis分布式锁 ...