Predict the output of following C++ programs.

Question 1

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

Question 2

  1. 1 #include<iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 class Test
  5. 5 {
  6. 6 private:
  7. 7 static int count;
  8. 8 public:
  9. 9 static Test& fun();
  10. 10 };
  11. 11
  12. 12 int Test::count = 0;
  13. 13
  14. 14 Test& Test::fun()
  15. 15 {
  16. 16 Test::count++;
  17. 17 cout << Test::count << " ";
  18. 18 return *this;
  19. 19 }
  20. 20
  21. 21 int main()
  22. 22 {
  23. 23 Test t;
  24. 24 t.fun().fun().fun().fun();
  25. 25 return 0;
  26. 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. 1 #include<iostream>
  2. 2 using namespace std;
  3. 3
  4. 4 class Test
  5. 5 {
  6. 6 private:
  7. 7 static int count;
  8. 8 public:
  9. 9 Test& fun();
  10. 10 };
  11. 11
  12. 12 int Test::count = 0;
  13. 13
  14. 14 Test& Test::fun()
  15. 15 {
  16. 16 Test::count++;
  17. 17 cout << Test::count << " ";
  18. 18 return *this;
  19. 19 }
  20. 20
  21. 21 int main()
  22. 22 {
  23. 23 Test t;
  24. 24 t.fun().fun().fun().fun();
  25. 25 return 0;
  26. 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. 小米多模网关接入Home Assistant ZNDMWG03LM

    一.小米zigbee网关使用 先下载米家app,打开手机蓝牙,登陆点"我的"界面,将网关设备插上电源,橙灯闪烁,点击蓝牙网关等待弹窗提示连接,选择连接路由器(需2.4GHz),输入 ...

  2. Spring Cloud Alibaba 使用Seata解决分布式事务

    为什么会产生分布式事务? 随着业务的快速发展,网站系统往往由单体架构逐渐演变为分布式.微服务架构,而对于数据库则由单机数据库架构向分布式数据库架构转变.此时,我们会将一个大的应用系统拆分为多个可以独立 ...

  3. redis 内存划分

    1.数据:作为数据库,数据是最主要的部分,这部分占用的内存会被统计在used_memory中 2.进程内存:redis主进程本身运行需要占用的内存,这部分内存会被统计在used_memory_rss中 ...

  4. [linux]centos7.4上升级python2版本到python3.6.5 【安装双版本,默认python3】

    版本声明 centos7.4 前言:linux上的python默认是版本2的,之前学django项目用的是3的版本 所以得升级下版本~ 1.下载python3.6.5 cd /usr/local/ w ...

  5. [前端随笔][Vue] 多级菜单实现思路——组件嵌套

    说在前面 本篇记录学习了vue-element-admin中的多级菜单的实现 [传送门] @vue/cli 4.2.2:vuex:scss:组件嵌套 正文 创建项目 npm create 项目名 // ...

  6. python 格式化输出详解(占位符:%、format、f表达式)——上篇 理论篇

    0 - 占位符介绍 要实现字符串的拼接,使用占位符是的一种高效.常用的方式. 举个例子,下面是不使用占位符的一种写法,直接使用加号拼接字符串 name = "Li hua" age ...

  7. 通过小乌龟从本地主机删除远端svn服务器端的代码或图片文件

    先说解决方案,如果通过windows的右键删除,再次Commit还是会下拉下来,所以需要通过小乌龟的删除来删除这个文件,在删除的那个文件夹里再次提交即可. 图片 1. 选中要删除的文件 2. 右键通过 ...

  8. [hdu7026]Might and Magic

    (以下默认$A_{0},D_{0},P_{0},K_{0}$都为非负整数) 显然存活轮数$S=\lceil\frac{H_{0}}{C_{p}\max(A_{1}-D_{0},1)}\rceil$​​ ...

  9. [loj2091]小星星

    (分别用$E_{T}$和$E_{G}$表示树和图的边集) 简单分析,可以发现题目即求排列$p_{i}$的数量,满足$\forall (x,y)\in E_{T},(p_{x},p_{y})\in E_ ...

  10. 『学了就忘』Linux用户管理 — 50、用户管理相关文件详细说明

    目录 1.用户信息文件 2./etc/shadow影子文件 3./etc/group 组信息文件 4.组密码文件 5.用户的家目录 6.用户邮箱目录 7.用户模板目录 总结: 提示:严格的用户权限划分 ...