Output of C++ Program | Set 7
Predict the output of following C++ programs.
Question 1
1 class Test1
2 {
3 int y;
4 };
5
6 class Test2
7 {
8 int x;
9 Test1 t1;
10 public:
11 operator Test1()
12 {
13 return t1;
14 }
15 operator int()
16 {
17 return x;
18 }
19 };
20
21 void fun ( int x)
22 {
23 }
24 void fun ( Test1 t )
25 {
26 }
27
28 int main()
29 {
30 Test2 t;
31 fun(t);
32 return 0;
33 }
Output: Compiler Error
There are two conversion operators defined in the Test2 class. So Test2 objects can automatically be converted to both int and Test1. Therefore, the function call fun(t) is ambiguous as there are two functions void fun(int ) and void fun(Test1 ), compiler has no way to decide which function to call.
In general, conversion operators must be overloaded carefully as they may lead to ambiguity.
Question 2
1 #include <iostream>
2 using namespace std;
3
4 class X
5 {
6 private:
7 static const int a = 76;
8 public:
9 static int getA()
10 {
11 return a;
12 }
13 };
14
15 int main()
16 {
17 cout <<X::getA()<<endl;
18 return 0;
19 }
Output: The program compiles and prints 76
Generally, it is not allowed to initialize data members in C++ class declaration, but static const integral members are treated differently and can be initialized with declaration.
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-27 15:35:28
Output of C++ Program | Set 7的更多相关文章
- Output of C++ Program | Set 18
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 17
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 16
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 15
Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...
- Output of C++ Program | Set 14
Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...
- Output of C++ Program | Set 13
Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...
- Output of C++ Program | Set 11
Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...
- Output of C++ Program | Set 9
Predict the output of following C++ programs. Question 1 1 template <class S, class T> class P ...
- Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...
随机推荐
- 1组-Alpha冲刺-4/6
一.基本情况 队名:震震带着六菜鸟 组长博客:https://www.cnblogs.com/Klein-Wang/p/15553196.html 小组人数:7人 二.冲刺概况汇报 王业震 过去两天完 ...
- requests之代理的使用
import requests # 访问url url = 'http://www.baidu.com/s?' # 请求头 headers = { 'User-Agent': 'Mozilla/5.0 ...
- CSS学习笔记:grid布局
目录 一.Grid布局简介 二.Grid布局的一些概念 三. 容器元素属性 1. grid-template-* 1.1 网格行和列的设置 1.2 repeat的使用 1.3 使用fr 1.4 aut ...
- K8S核心概念之SVC(易混淆难理解知识点总结)
本文将结合实际工作当中遇到的一些问题和情况来解析SVC的作用以及一些比较易混淆和难理解的概念,方便日后工作用到或者遗忘时可以直接在自己曾经学习总结的博客当中直接查找到. 首先应该清楚SVC的作用是什么 ...
- python地理空间(1)--概念引入
1 python与地理空间分析 1.1 与我们的生活 ushahidi是一个优秀的地理空间地图应用,回寝FQ看一下. ushahidi有一个python库-ushapy 地理空间救灾建模程序是最近比较 ...
- [atAGC055B]ABC Supremacy
将第$i$个字符在$A->C->B->A$这个环上操作$i$次,而此时的操作也即将$AAA,BBB$或$CCC$变为其中的另一个字符串 通过操作$XXXY->YYYY-> ...
- [loj3528]位移寄存器
当$s=0$时(求最小值): 若$x_{0},x_{1},...,x_{n-1}$和$y_{0},y_{1},...,y_{n-1}$像题中所给的方式存储在$r[0][0..nk-1]$和$r[1][ ...
- 在安卓开发中需要格式化桌面icon图标
使用以下在线工具即可实现http://www.makeicon.cc/home/index
- 从零开始,使用Dapr简化微服务
序言 现有的微服务模式需要再业务代码中集成大量基础设施模块,比如注册中心,服务发现,服务调用链路追踪,请求熔断,重试限流等等,使得系统过于臃肿重量级. Dapr作为新一代微服务模式,使用sidecar ...
- P6072 『MdOI R1』Path
考虑我们有这样操作. 我们只要维护两点在子树内和两点在子树外的异或和即可. 前者可以类似于线段树合并的trie树合并. 后者有两种做法: 一种是把dfn序翻倍:然后子树补变成了一个区间最大异或问题,可 ...