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. 该虚拟机似乎正在使用中。如果该虚拟机未在使用,请按“获取所有权(T)”按钮获取它的所有权

    问题 打开虚拟机镜像时报 VMware该虚拟机似乎正在使用中.如果该虚拟机未在使用,请按"获取所有权(T)"按钮获取它的所有权 解决方法 在你安装的镜像文件目录下找到后缀为.vmx ...

  2. python unicode escape

    from: https://stackoverflow.com/questions/44742806/how-to-remove-escape-characters-escaping-unicode- ...

  3. 18-Spring Cloud Alibaba Nacos

    简介 为什么叫Nacos 前四个字母分别为Naming和Configuration的前两个字母,最后的s为Service Nacos是什么 一个更易于构建云原生应用的动态服务发现.配置管理和服务管理平 ...

  4. 菜鸡的Java笔记 Eclipse 的使用

    Eclipse 的使用    1. Eclipse 简介    2. Eclipse 中的JDT 的使用    3. Eclipse 中的使用 junit 测试        Eclipse (中文翻 ...

  5. [loj3069]整点计数

    1.基础知识 定义 定义1.1(高斯整数):$\mathbb{Z}[i]=\{a+bi\mid a,b\in Z\}$(其中$i$为虚数单位,即$i^{2}=-1$) 定义1.2(范数):$N(\al ...

  6. 阿里云服务器的MySQL连接和vscode远程连接

    目录 一.前言 二.使用Navicat等软件连接MySQL 1. 修改服务器系统密码 2. 防火墙选项添加MySQL 3. 使用Navicat连接 三.使用vscode连接服务器 一.前言 双十一的时 ...

  7. 【Tool】IntelliJ 搭建Node.js环境

    IntelliJ IDEA 开发 Node.js 2019-07-29  14:12:34  by冲冲 1. 配置插件 在IDEA的 file -> setting -> Plugins, ...

  8. html+css第八篇滑动门和可爱的css精灵

    滑动门: 滑动门并不是一项全新的技术,它是利用背景图像的可层叠性,并允许他们在彼此之上进行滑动,以创造一些特殊的效果. CSS精灵 CSS Sprites在国内很多人叫CSS精灵,是一种网页图片应用处 ...

  9. python-django-分页处理

    每个模型都有一个管理器,Manager 通过分页可以先加载一部分的数据内容,然后避免大量的查询带来的等待时间 应用场景是,排行榜,前十条或者 for i in range(100): ...: ... ...

  10. python-django使用ORM模型增删改查CRUD

    from weibo.models import WeiboUser as User user_obj = User.objects.get(pk=1) user_obj.pk Out[4]: 1 u ...