zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    前端git开发分支各种场景管理
    RxJS Subject学习
    微信小程序登陆流程(20200322)
    vue依赖收集的策略
    eggjs2.x版本异步获取config配置方案
    dubbo连接过程
    计算机中对流的理解
    Egg.js运行环境配置场景
    Promise和Observable的映射
    eggjs异常捕获机制
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3445882.html
Copyright © 2011-2022 走看看