Output of C++ Program | Set 9
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的更多相关文章
- 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 7
Predict the output of following C++ programs. Question 1 1 class Test1 2 { 3 int y; 4 }; 5 6 class T ...
- Output of C++ Program | Set 6
Predict the output of below C++ programs. Question 1 1 #include<iostream> 2 3 using namespace ...
随机推荐
- journalctl常用命令
journalctl -xe 查看全部日志# 以flow形式查看日志 $ journalctl -f # 查看内核日志 $ journalctl -k # 查看指定服务日志 $ journalctl ...
- k8s入坑之路(8)kube-proxy详解
kube-proxy 每台机器上都运行一个 kube-proxy 服务,它监听 API server 中 service 和 endpoint 的变化情况,并通过 iptables 等来为服务配置负载 ...
- SpringBoot2.x请求注解简单介绍(4)
1.新建项目,项目中实战讲解注解作用 2.pom.xml依赖配置 <properties> <project.build.sourceEncoding>UTF-8</pr ...
- jenkins 安装与使用
1.jenkins下载:https://jenkins.io/zh/download/ 2.将下载好的war包放到tomcat容器下的D:\apache-tomcat-9.0.10\webapps下( ...
- 攻防世界 WEB 高手进阶区 TokyoWesterns CTF shrine Writeup
攻防世界 WEB 高手进阶区 TokyoWesterns CTF shrine Writeup 题目介绍 题目考点 模板注入 Writeup 进入题目 import flask import os a ...
- 17.彻底解决Jmap在mac版本无法使用的问题
彻底解决Jmap在mac版本无法使用的问题 看了网上很多帖子,都说一半,说的都是大家说过的,根本没有解决问题.说jdk8不行,换成jdk9或者jdk11,我都试了,还是不行,最后说是mac的问题.换成 ...
- Python之模块导入(不看会后悔系列)
看到这个标题猜想大家内心OS: 什么辣鸡水文,划走划走~ 别急有干货! 静态导入(照顾新人) 假设现在有两个文件a,b在不同目录,b文件想引用a文件中的函数: # test_module/sub_mo ...
- JDK源码阅读(5):HashTable类阅读笔记
HashTable public class Hashtable<K,V> extends Dictionary<K,V> implements Map<K,V>, ...
- 手撸一个IOC容器
IoC 什么是IoC? IoC是Inversion of Control(控制反转)的简称,注意它是一个技术思想.描述的是对象创建.管理的事情. 传统开发方式:比如类A依赖类B,往往会在类A里面new ...
- [python]Appium+python +pytest 实现APP自动化,基于安卓
1.安卓环境搭建 &关于app自动化,个人觉得安装过程比较复杂,脚本难度实现和web自动化差不多封装关键字即可,因此,下面会写安装.启动APP以及过程中遇到的一些坑(这一篇偏向解释给个人) & ...