Predict the output of following C++ program.

 1 #include <iostream>
2
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 // A method to compare two Complex numbers
18 bool operator == (Complex rhs)
19 {
20 return (real == rhs.real && imag == rhs.imag)? true : false;
21 }
22 };
23
24 int main()
25 {
26 // a Complex object
27 Complex com1(3.0, 0.0);
28
29 if (com1 == 3.0)
30 cout << "Same";
31 else
32 cout << "Not Same";
33 return 0;
34 }

  Output: The program compiles fine and produces following output.

  Same
  

  In C++, if a class has a constructor which can be called with a single argument, then this constructor becomes conversion constructor because such a constructor allows conversion of the single argument to the class being constructed.
  We can avoid such implicit conversions as these may lead to unexpected results. We can make the constructor explicit with the help of explicit keyword.

  For example, if we try the following program that uses explicit keyword with constructor, we get compilation error.

 1 #include <iostream>
2 using namespace std;
3
4 class Complex
5 {
6 private:
7 double real;
8 double imag;
9
10 public:
11 // Default constructor
12 explicit Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)
13 {
14 }
15
16 // A method to compare two Complex numbers
17 bool operator== (Complex rhs)
18 {
19 return (real == rhs.real && imag == rhs.imag)? true : false;
20 }
21 };
22
23 int main()
24 {
25 // a Complex object
26 Complex com1(3.0, 0.0);
27
28 if (com1 == 3.0)
29 cout << "Same";
30 else
31 cout << "Not Same";
32 return 0;
33 }

  Output: Compiler Error

  no match for 'operator==' in 'com1 == 3.0e+0'
  

  We can still typecast the double values to Complex, but now we have to explicitly typecast it.

  For example, the following program works fine.

 1 #include <iostream>
2 using namespace std;
3
4 class Complex
5 {
6 private:
7 double real;
8 double imag;
9
10 public:
11 // Default constructor
12 explicit Complex(double r = 0.0, double i = 0.0) : real(r), imag(i)
13 {
14 }
15
16 // A method to compare two Complex numbers
17 bool operator== (Complex rhs)
18 {
19 return (real == rhs.real && imag == rhs.imag)? true : false;
20 }
21 };
22
23 int main()
24 {
25 // a Complex object
26 Complex com1(3.0, 0.0);
27
28 if (com1 == Complex(3.0))
29 cout << "Same";
30 else
31 cout << "Not Same";
32 return 0;
33 }

  Output: The program compiles fine and produces following output.

  Same

  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:41:25

Use of explicit keyword in C++的更多相关文章

  1. Explicit keyword

    说实话,从来没有感觉到这个keyword实用,直到今天. explicit的意思是明显的,和它相相应的一个词是implicit意思是隐藏的. 我參考了MSDN和<c++标准程序库>对这个k ...

  2. C# 自己定义 implicit和explicit转换

    explicit 和 implicit 属于转换运算符,如用这两者能够让我们自己定义的类型支持相互交换explicti 表示显式转换.如从 A -> B 必须进行强制类型转换(B = (B)A) ...

  3. Explicit

    Prefixing the explicit keyword to the constructor prevents the compiler from using that constructor ...

  4. explicit 构造函数

    一.构造函数.默认构造函数.合成的默认构造函数 构造函数,是函数名与类名同样.没有返回类型的特殊的成员函数.能够有初始化列表. 默认构造函数,没有形參.或全部形參都有默认实參的构造函数. 假设没有显示 ...

  5. C++ essentials 之 explicit constructor

    这篇博客的源起是我下面的一段代码 #include <bits/stdc++.h> using namespace std; int main(){ priority_queue<l ...

  6. Google C++ Style Guide

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

  7. 9.Methods(二)

    4.Operator Overload Methods allow a type to define how operators should manipulate instances of the ...

  8. Learn Python More

    0, 看了一个python项目开源源码, 才知道现在这点python知识实在是弱爆了.. 尼玛就像学了2500个常用汉字, 然后要去理解"楚辞".. 代码如下, 解释一点一点从网上 ...

  9. backref 用法

    源码 def backref(name, **kwargs): """Create a back reference with explicit keyword argu ...

随机推荐

  1. 攻防世界 Misc 新手练习区 stegano CONFidence-DS-CTF-Teaser Writeup

    攻防世界 Misc 新手练习区 stegano CONFidence-DS-CTF-Teaser Writeup 题目介绍 题目考点 隐写术 摩斯密码 Writeup 下载附件是PDF文件打开,研究一 ...

  2. 4. 理解Update、Enter、Exit 与 添加、删除元素

    理解Update.Enter.Exit 与 添加.删除元素 在使用data()绑定数据时,例如:现在我们有一个数组[3,6,9,12,15],我们可以将数组每一项与一个<p>绑定,但是,现 ...

  3. MyBatis Plus中使用and和or

    如图:show me the code 参考: https://mp.baomidou.com/guide/wrapper.html#or

  4. 3D 穿梭效果?使用 UWP 也能搞定

    昨天 ChokCoco 大佬搞了个 3D 穿梭效果出来,具体可见这里: 3D 穿梭效果?使用 CSS 轻松搞定 这个效果太神奇了,他还问我能不能用 WPF 搞出来,因为我完全没用过 WPF 的 3D, ...

  5. vue 快速入门 系列 —— 使用 vue-cli 3 搭建一个项目(上)

    其他章节请看: vue 快速入门 系列 使用 vue-cli 3 搭建一个项目(上) 前面我们已经学习了一个成熟的脚手架(vue-cli),笔者希望通过这个脚手架快速搭建系统(或项目).而展开搭建最好 ...

  6. 反射的妙用:C#通过反射动态生成类型继承接口并实现

    起因 最近想自己鼓捣个RPC,想着简化RPC调用方式,直接申明接口,然后根据接口的属性去配置RPC调用的相关信息.有一种说法叫申明式调用. 简单来说就是,申明一个interface,动态继承并实例化, ...

  7. SVD专题1 算子的奇异值分解——矩阵形式的推导

    目录 SVD专题1 算子的奇异值分解--矩阵形式的推导 前言 Preface 几点说明 预备知识 Prerequisite 1.1 极分解 Polar Decomposition 1.2 等距同构 U ...

  8. vue3 学习笔记 (二)——axios 的使用有变化吗?

    本篇文章主要目的就是想告诉我身边,正在学 vue3 或者 准备学 vue3 的同学,vue3中网络请求axios该如何使用,防止接触了一点点 vue3 的同学会有个疑问?生命周期.router .vu ...

  9. Study Blazor .NET(四)数据绑定

    翻译自:Study Blazor .NET,转载请注明. 数据绑定 单向绑定 在blazor中单向绑定简单而直接,无需UI刷新或渲染.下面示例展示了单向数据绑定: //Counter.razor @p ...

  10. layui的入门使用

    1.如果使用单独的layui插件的话需要先引入jquery的插件,官方建议1.8+的版本. 2.引入后就能根据规则正常使用了.