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

  • 相关阅读:
    每天一个linux命令(文件操作):【转载】find 命令的参数详解
    每天一个linux命令(文件操作):【转载】find命令之xargs
    每天一个linux命令(文件操作):【转载】find命令之exec
    每天一个linux命令(文件操作):【转载】find 命令概览
    每天一个linux命令(文件操作):【转载】locate命令
    js 常见bug
    form表单 post 请求打开新页面
    vue 鼠标移入移出 列表蒙层展示
    js 递归总结
    新编家庭医生大全集
  • 原文地址:https://www.cnblogs.com/iloveyouforever/p/3445882.html
Copyright © 2011-2022 走看看