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 ...
随机推荐
- Java测试开发--Spring Tools Suite (STS) 简介(一)
sts是一个定制版的Eclipse,专为Spring开发定制的,方便创建调试运行维护Spring应用. 官网下载之后,可以看到一个sts-bundle,里面有三个文件夹,一个法律信息,一个tc Ser ...
- RabbitMQ 处理过慢,原来是一个 SQL 缓存框架导致的 GC 频繁触发
一:背景 1. 讲故事 上个月底,有位朋友微信找到我,说他的程序 多线程处理 RabbitMQ 时过慢,帮忙分析下什么原因,截图如下: 这问题抛出来,有点懵逼,没说CPU爆高,也没说内存泄漏,也没说程 ...
- java中static关键字的解析
静态的特点: A:随着类的加载而加载 B:优先于对象而存在 C:静态是被所有对象共享的数据 这也是我们来判断是否使用静态的标准 D:静态的出现,让我们的调用方式多了一种 类名.静态的内容 非静态的内容 ...
- <C#任务导引教程>练习十
/*83,使用接口完成多继承问题 简化版*/using System;interface ITeacher{ string Name { get; set; ...
- [atARC126F]Affine Sort
记$g(k)$为$c$恰为$k$的合法三元组数,显然$f(k)=\sum_{i=1}^{k}g(i)$ 结论:若$\lim_{k\rightarrow \infty}\frac{g(k)}{k^{2} ...
- [loj3014]独特的城市
约定:一棵树的深度定义为其中到根最远的点到根的距离 考虑节点$x$的答案: 任取一条直径,根据直径的性质,到$x$较远的直径端点一定是到$x$最远的点之一 由此,不难证明对于$x$独特的点,一定在$x ...
- Go语言核心36讲(Go语言实战与应用十二)--学习笔记
34 | 并发安全字典sync.Map (上) 我们今天再来讲一个并发安全的高级数据结构:sync.Map.众所周知,Go 语言自带的字典类型map并不是并发安全的. 前导知识:并发安全字典诞生史 换 ...
- git使用大全
创建四个分支: 查看分支:git branch查看仓库里面所有的分支 git branch -a刷新分支 git remote update origin --prune创建分支:git branch ...
- html+css第六篇-定位
relative相对定位/定位偏移量 position:relative; 相对定位 a.不影响元素本身的特性: b.不使元素脱离文档流: c.如果没有定位偏移量,对元素本身没有任何影响: 定位元素位 ...
- 【机器学习与R语言】9- 支持向量机
目录 1.理解支持向量机(SVM) 1)SVM特点 2)用超平面分类 3)对非线性空间使用核函数 2. 支持向量机应用示例 1)收集数据 2)探索和准备数据 3)训练数据 4)评估模型 5)提高性能 ...