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. OOP作业总结一

    PS:建议用 Edge 查看此博客,Chrome 的话文章界面会有点窄,看起来可能会比较难受,我想改宽点但是不会改. 我会改了!改宽了一些,现在看起来舒服了很多,芜湖. 问题数据已修复,我们胜利辣! ...

  2. 记一次 Java 导出大批量 Excel 优化

    常用的excel导出方案,详情见Spring Boot 入门(十二):报表导出,对比poi.jxl和esayExcel的效率,其中jxl.esayEscel 底层都是基于 poi,它们仅仅是对 poi ...

  3. pip 更新方法

    使用python -m pip install --upgrade pip 使用python -m pip install -U --force-reinstall pip 使用pip install ...

  4. Flink sql 之AsyncIO与LookupJoin的几个疑问 (源码分析)

    本文源码基于flink 1.14 被同事问到几个关于AsyncIO和lookUp维表的问题所以翻了下源码,从源码的角度解惑这几个问题 对于AsyncIO不了解的可以看看之前写的这篇  <Flin ...

  5. IDEA中三种注释方式的快捷键

    三种注释方式   行注释.块注释.方法或类说明注释. 一.快捷键:Ctrl + /   使用Ctrl+ /, 添加行注释,再次使用,去掉行注释 二.演示代码 if (hallSites != null ...

  6. xpath解析案例

    xpath解析百度页面的百度一下 # 1)获取网页的源码 # 2)解析的服务器响应的文件 etree.HTML , 用来解析字符串格式的HTML文档对象,将传进去的字符串转变成 element 对象 ...

  7. vue开发中的一些简单骚操作

    在开发过程中,我们可以定义很多参数,这时需要通过不同的操作来改变不同的参数,这就比较复杂了, 虽然不难,但是代码多了也不好看,这时我们就可以通过简单的操作就行简化: 1.对象使用方括号 let obj ...

  8. tomcat隐藏版本号

    默认报错页面信息会暴露出版本号 进入tomcat的lib目录找到catalina.jar文件 unzip catalina.jar之后会多出两个文件夹 进入org/apache/catalina/ut ...

  9. Vue安装Vue Devtools调试工具提示 Error: Cannot find module '@vue-devtools/build-tools' 解决办法

    我看网络上面安装Vue Devtools 调试工具的步骤几乎都是按照文章链接里的步骤进行安装, 但是在最终执行编译命令的时候 :npm run build ,提示如下错误: 尝试了很多方法,都不能解决 ...

  10. 7.1 k8s使用configmapg 给pod内的程序提供配置文件

    ConfigMap 是一种 API 对象,用来将非机密性的数据保存到键值对中.使用时, Pods 可以将其用作环境变量.命令行参数或者存储卷中的配置文件. 以下以nginx镜像提供配置文件为例镜像演示 ...