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

  • 相关阅读:
    Eclipse中输入系统变量和运行参数
    poi大数据导入解决方法
    技术走向管理一些思考(2)-建立管理思维
    C. Diverse Permutation(Codeforces Round #275(div2)
    mysql大数据高并发处理
    UVa 11849
    centos7;windows下安装和使用spice
    HDU-4819-Mosaic(二维线段树)
    Using a Plugin
    CSU1608: Particle Collider(后缀数组)
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3445882.html
Copyright © 2011-2022 走看看