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 ...
随机推荐
- 虚拟化与kvm
cpu指令级别 传统中操作系统运行于R0中称之为特权级别,直接与硬件进行交互. 应用程序运行于r3级别称之为低权限,无法与硬件直接进行交互.也就是说程序是运行于用户态,系统运行于内核态中. 虚拟化要解 ...
- JavaScript事件捕获冒泡与捕获
事件流 JavaScript中,事件流指的是DOM事件流. 概念 事件的传播过程即DOM事件流.事件对象在 DOM 中的传播过程,被称为"事件流".举个例子:开电脑这个事,首先你是 ...
- Linux usb 6. HC/UDC 测试
目录 1. 背景介绍 2. Device (gadget zero) 2.1 gadget zero 创建 2.2 SourceSink Function 2.3 Loopback Function ...
- Rancher 下图形界面 搭建 K8S 集群
首先我们准备4台 2核3G 的 centos 7 温馨提示:先安装好一台 CentOS 的虚拟机,并且安装好 docker,永久关闭防火墙. 再这个基础上我们分别克隆出四台 Rancher.K8S1. ...
- docker file 笔记
FROM # FROM scratch, FROM centos, FROM ubuntu:latest LABEL RUN # 每运行一次RUN,image都会生成新的一层,为了美观,避免 ...
- SpringBoot Actuator — 埋点和监控
项目中看到了有埋点监控.报表.日志分析,有点兴趣想慢慢捣鼓一下 1. 数据埋点 监控机器环境的性能和业务流程或逻辑等各项数据,并根据这些数据生成对应的指标,那么我们就称为数据埋点.比如我们想知道某个接 ...
- silky微服务模块
目录 模块的定义和类型 在模块中注册服务 通过ServiceCollection实现服务注册 通过ContainerBuilder实现服务注册 使用模块初始化任务 使用模块释放资源 模块的依赖关系 构 ...
- vue如何写组件(script标签引入的方式)
很多人知道.vue结构的单文件组件形式,不过这种单文件组件的结构如果要加入到现有的jquery项目中就比较麻烦了,那如果我们又想用vue来写模板,又不想引入vue-cli管理,那该怎么来写组件呢?别着 ...
- 菜鸡的Java笔记 笔记
// 雇员编号 姓名 职位 基本工资 佣金等信息 package study; class Enr{ private int number; // 编号 private String fullName ...
- 菜鸡的Java笔记 第二十三 - java 抽象类的概念
abstractClass 抽象类的概念 1.抽象类的基本定义 2.抽象类的使用原则 不会抽象类与接口,java = 没学 ...