Predict the output of following C++ programs.

Question 1

 1 template <class S, class T> class Pair
2 {
3 private:
4 S x;
5 T y;
6 /* ... */
7 };
8
9 template <class S> class Element
10 {
11 private:
12 S x;
13 /* ... */
14 };
15
16 int main ()
17 {
18 Pair <Element<int>, Element<char>> p;
19 return 0;
20 }

  Output:

  Compiler Error: '>>' should be '> >' within a nested template argument list
  When we use nested templates in our program, we must put a space between two closing angular brackets, otherwise it conflicts with operator >>.

  For example, following program compiles fine.

 1 template <class S, class T> class Pair
2 {
3 private:
4 S x;
5 T y;
6 /* ... */
7 };
8
9 template <class S> class Element
10 {
11 private:
12 S x;
13 /* ... */
14 };
15
16 int main ()
17 {
18 Pair <Element<int>, Element<char> > p; // note the space between '>' and '>'
19 return 0;
20 }

Question 2

 1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 static int count;
8 public:
9 static Test& fun();
10 };
11
12 int Test::count = 0;
13
14 Test& Test::fun()
15 {
16 Test::count++;
17 cout << Test::count << " ";
18 return *this;
19 }
20
21 int main()
22 {
23 Test t;
24 t.fun().fun().fun().fun();
25 return 0;
26 }

  Output:

  Compiler Error: 'this' is unavailable for static member functions
  this pointer is not available to static member methods in C++, as static methods can be called using class name also. Similarly in Java, static member methods cannot access this and super (super is for base or parent class).
  If we make fun() non-static in the above program, then the program works fine.

 1 #include<iostream>
2 using namespace std;
3
4 class Test
5 {
6 private:
7 static int count;
8 public:
9 Test& fun();
10 };
11
12 int Test::count = 0;
13
14 Test& Test::fun()
15 {
16 Test::count++;
17 cout << Test::count << " ";
18 return *this;
19 }
20
21 int main()
22 {
23 Test t;
24 t.fun().fun().fun().fun();
25 return 0;
26 }

  Output:
  1 2 3 4

  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:48:38

Output of C++ Program | Set 9的更多相关文章

  1. Output of C++ Program | Set 18

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  2. Output of C++ Program | Set 17

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  3. Output of C++ Program | Set 16

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  4. Output of C++ Program | Set 15

    Predict the output of following C++ programs. Question 1 1 #include <iostream> 2 using namespa ...

  5. Output of C++ Program | Set 14

    Predict the output of following C++ program. Difficulty Level: Rookie Question 1 1 #include <iost ...

  6. Output of C++ Program | Set 13

    Predict the output of following C++ program. 1 #include<iostream> 2 using namespace std; 3 4 c ...

  7. Output of C++ Program | Set 11

    Predict the output of following C++ programs. Question 1 1 #include<iostream> 2 using namespac ...

  8. 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 T ...

  9. Output of C++ Program | Set 6

    Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...

随机推荐

  1. 设计模式学习-使用go实现单例模式

    单例模式 定义 优点 缺点 适用范围 代码实现 懒汉模式 饿汉模式 双重检测 sync.Once 参考 单例模式 定义 什么是单例模式:保证一个类仅有一个实例,并提供一个全局访问它的全局访问点. 例如 ...

  2. node.js中模块和包

    node.js中模块和包 什么是模块 如何创建并加载模块 1. 创建模块 2. 单次加载 3. 覆盖 exports 如何创建一个包 1. 作为文件夹的模块 2. package.json 如何使用包 ...

  3. Linux mem 2.5 Buddy 内存回收机制

    文章目录 1. 简介 2. LRU 组织 2.1 LRU 链表 2.2 LRU Cache 2.3 LRU 移动操作 2.3.1 page 加入 LRU 2.3.2 其他 LRU 移动操作 3. LR ...

  4. shiro550反序列化分析

    拖了很久的shiro分析 漏洞概述 Apache Shiro <= 1.2.4 版本中,加密的用户信息序列化后存储在Cookie的rememberMe字段中,攻击者可以使用Shiro的AES加密 ...

  5. 深入理解Spring IOC容器

    本文将从纯xml模式.xml和注解结合.纯注解的方式讲解Spring IOC容器的配置和相关应用. 纯XML模式 实例化Bean的三种方式: 使用无参构造函数 默认情况下,会使用反射调用无参构造函数来 ...

  6. Java学习(十五)

    下棋又被暴打 所以心态有点爆炸,学完习又去打云顶,忘记写博客了... 所以今天也有点水了,这下棋真是搞崩我心态了. Java学了函数重载 大概的要点在这 还有Web的学习,否定伪类. 如 p:not( ...

  7. request模块做post请求时,body为json格式,并且带有中文,如何请求

    后台接口只能解析json,并且一定要是中文才能解析出来,如果是unicode编码的中文则会报错 看requests的源码.以下为解决方法: #将requests库中的models.py文件中的第461 ...

  8. vue2与vue3的差异(总结)?

    vue作者尤雨溪在开发 vue3.0 的时候开发的一个基于浏览器原生 ES imports 的开发服务器(开发构建工具).那么我们先来了解一下vite Vite Vite,一个基于浏览器原生 ES i ...

  9. 路径前面加与不加"/"的区别

    加"/"是绝对路径, 不加"/"是相对路径.假设你的这个html文件的路径是www.example.com/path/to/html/a.html,那么src= ...

  10. 最大连续子数组和--dp

    最大连续子数组和 问题: 给定n个整数(可能为负数)组成的序列a[1],a[2],a[3],-,a[n],求该序列如a[i]+a[i+1]+-+a[j]的子段和的最大值.当所给的整数均为负数时定义子段 ...