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 ...
随机推荐
- Spring Cloud Gateway 整合阿里 Sentinel网关限流实战!
大家好,我是不才陈某~ 这是<Spring Cloud 进阶>第八篇文章,往期文章如下: 五十五张图告诉你微服务的灵魂摆渡者Nacos究竟有多强? openFeign夺命连环9问,这谁受得 ...
- 🔥完美解决ESlint+Prettier各项配置冲突的语法报错问题(新手向)
本文重点: 1.解决修改了Prettier默认配置,项目内格式化无法生效 2.解决Prettier缺少配置,函数名和括号之间,自动添加空格 3.settings.json配置项分享 一个程序员,可能非 ...
- CSS学习(三)特指度和层叠
一.特指度 特制度的一般形式是0,0,0,0 行内样式,第一位的特指度加一 id选择符,第二位的特指度加一 类选择符.属性选择符.伪类,第三位的特指度加一 元素选择符.伪元素,第四位的特指度加一 特指 ...
- Abp Vnext Vue3 的版本实现
基于ABP Vnext的二次开发,前端 vue3.0,Typescript,Ant Design Vue ,Vben Admin 的后台管理框架. 技术点 Net Core5.0 ABP Vnext ...
- java+selenium+testNG+Allure报表【新增截图到报表功能】
1.pom.xml配置 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://w ...
- Spark的安装及其配置
1.Spark下载 https://archive.apache.org/dist/spark/ 2.上传解压,配置环境变量 配置bin目录 解压:tar -zxvf spark-2.4.5-bin- ...
- TLFS 内存分配算法详解
文章目录 1. DSA 背景介绍 1.1 mmheap 1.2 mmblk 2. TLFS 原理 2.1 存储结构 2.2 内存池初始化 2.3 free 2.4 malloc 参考资料 1. DSA ...
- hudi clustering 数据聚集(三 zorder使用)
目前最新的 hudi 版本为 0.9,暂时还不支持 zorder 功能,但 master 分支已经合入了(RFC-28),所以可以自己编译 master 分支,提前体验下 zorder 效果. 环境 ...
- m3u8 ts 视频流爬取思路,合成
.... 先开调试,输入查找一下有没有 m3u8 文件 然后下下来用Notepad++ 打开一下 (以下的样子) 这里就是整个视频的视频流, .ts 的都是文件,都下下来, ------------ ...
- 菜鸡的Java笔记 实践 - java 数组操作
讲解一个继承的实现思路 要求定义一个数组操作类(Array类),在这个类里面可以进行整型数组的操作,由外部传入数组的大小 ,并且要求实现数据的保存以及数据的 ...