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 ...
随机推荐
- Redis网络库源码分析(1)之介绍篇
一.前言 Redis网络库是一个单线程EPOLL模型的网络库,和Memcached使用的libevent相比,它没有那么庞大,代码一共2000多行,因此比较容易分析.其实网上已经有非常多有关这个网络库 ...
- 小入门 Django(做个疫情数据报告)
Django 是 Python web框架,发音 [ˈdʒæŋɡo] ,翻译成中文叫"姜狗". 为什么要学框架?其实我们自己完全可以用 Python 代码从0到1写一个web网站, ...
- 《手把手教你》系列技巧篇(三十九)-java+ selenium自动化测试-JavaScript的调用执行-上篇(详解教程)
1.简介 在做web自动化时,有些情况selenium的api无法完成,需要通过第三方手段比如js来完成实现,比如去改变某些元素对象的属性或者进行一些特殊的操作,本文将来讲解怎样来调用JavaScri ...
- sm2加密
import java.math.BigInteger; import java.security.NoSuchAlgorithmException; import java.security.Sec ...
- 《手把手教你》系列技巧篇(四十一)-java+ selenium自动化测试 - 处理iframe -上篇(详解教程)
1.简介 原估计宏哥这里就不对iframe这个知识点做介绍和讲解了,因为前边的窗口切换就为这种网页处理提供了思路,另一个原因就是虽然iframe很强大,但是现在很少有网站用它了.但是还是有小伙伴或者童 ...
- 设计模式学习-使用go实现装饰模式
装饰器模式 定义 代码实现 优点 缺点 适用范围 装饰器模式和桥接模式的区别 参考 装饰器模式 定义 装饰模式:动态的给一些对象添加额外的职责,就增加功能来说,装饰模式比生成子类更灵活. 举个栗子: ...
- java更开源-安全可靠国产系统背景下的应有.NET Core的一席之地
"安可"背景下的中国软件开发 在中美当前背景下的安全可靠国产系统(简称安可),安可产业要实现技术自主可控,需要在四个层面逐步实现:基础硬件设施,如芯片.服务器.存储.交换机.路由器 ...
- 在Jenkins中执行 PowerShell 命令实现高效的CD/CI部署
相比于cmd,powershell支持插件.语法扩展和自定义扩展名,是智能化部署中闪闪的新星,越来越多的开发者偏爱使用Powershell. 如何让Jenkins支持Powershell呢?本文即展开 ...
- 如何提高C# StringBuilder的性能
本文探讨使用C# StringBuilder 的最佳实践,用于减少内存分配,提高字符串操作的性能. 在 .NET 中,字符串是不可变的类型.每当你在 .NET 中修改一个字符串对象时,就会在内存中创建 ...
- Asp.net core自定义依赖注入容器,替换自带容器
依赖注入 在asp.net core程序中,众所周知,依赖注入基本上贯穿了整个项目,以通用的结构来讲解,控制器层(Controller层)依赖业务层(Service层),业务层依赖于仓储层(Repos ...