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中存放的都是热点数据
当redis使用的内存超过了设置的最大内存时,会触发redis的key淘汰机制,在redis 3.0中有6种淘汰策略: noeviction: 不删除策略.当达到最大内存限制时, 如果需要使用更多内存 ...
- Redis高可用方案哨兵机制------ 配置文件sentinel.conf详解
Redis的哨兵机制是官方推荐的一种高可用(HA)方案,我们在使用Redis的主从结构时,如果主节点挂掉,这时是不能自动进行主备切换和通知客户端主节点下线的. Redis-Sentinel机制主要用三 ...
- 大一C语言学习笔记(1)---编译顺序问题;不同数据类型赋值,运算问题;算数运算符易错点(以解一元二次方程为例)
废话少说,上代码: #include<stdio.h> #include<math.h> int main()//解一元二次方程 { int a,b,c; double too ...
- SpringCloud微服务实战——搭建企业级开发框架(十六):集成Sentinel高可用流量管理框架【自定义返回消息】
Sentinel限流之后,默认的响应消息为Blocked by Sentinel (flow limiting),对于系统整体功能提示来说并不统一,参考我们前面设置的统一响应及异常处理方式,返回相同的 ...
- c++ 中vector 常见用法(给初学者)
c++ 中 vector vector有两个参数,一个是size,表示当前vector容器内存储的元素个数,一个是capacity,表示当前vector在内存中申请的这片区域所能容纳的元素个数. ca ...
- 应用SpringAOP及Tlog工具完成日志链路追踪、收集、持久化
一.痛点 目前我司各系统的日志管理比较原始,使用logback打日志到log文件,虽然有服务管理平台,但记录的日志也仅仅是前置机调用后台系统的出入参,当遇到问题时查日志较为麻烦. 登录VPN-打开服务 ...
- 国内首家!腾讯云正式成为 FinOps 基金会顶级会员
11月24日,腾讯云正式宣布加入FinOps基金会,作为国内首家FinOps基金会顶级会员,腾讯云将联合FinOps基金会,全面推进对FinOps标准和最佳实践的贡献,为企业提供云财务管理的最佳解决方 ...
- SpringCloud微服务实战——搭建企业级开发框架(二十二):基于MybatisPlus插件TenantLineInnerInterceptor实现多租户功能
多租户技术的基本概念: 多租户技术(英语:multi-tenancy technology)或称多重租赁技术,是一种软件架构技术,它是在探讨与实现如何于多用户的环境下共用相同的系统或程序组件,并且 ...
- patch增量更新
- List集合与Set集合(ArrayList,LinkedList,Vector,HashSet,LinkedHashSet,可变参数)
List集合介绍及常用方法 import java.util.ArrayList; import java.util.Iterator; import java.util.List; /* java. ...