搜索
熱搜: 活動 交友 discuz
查看: 3304|回復: 0
打印 上一主題 下一主題

[教學] ■C++ Primer p554

[複製鏈接]
跳轉到指定樓層
1#
發表於 2007-9-4 09:09:54 | 只看該作者 回帖獎勵 |倒序瀏覽 |閱讀模式
■C++ Primer p554
主題:function try block
測試結果:VC6[x]  BCB4[x]  G++[o]
實例:

#001 #include <iostream>
#002 using namespace std;
#003
#004 class popOnEmpty { /* ... */ };
#005 class pushOnFull { /* ... */ };
#006
#007 int main()
#008 try {
#009     throw popOnEmpty();
#010     throw pushOnFull();
#011     return 0;
#012 }
#013 catch ( pushOnFull ) {
#014     cout << "catch pushOnFull" << endl;
#015 }
#016 catch ( popOnEmpty ) {
#017     cout << "catch popOnEmpty" << endl;   // <-- 執行結果:此行。
#018 }


■C++ Primer  p564
主題:exception specification
測試結果:BCB4 表現佳,G++ 尚可,VC6 粗糙
實例:

#001 #include <iostream>
#002 using namespace std;
#003
#004 class popOnEmpty { /* ... */ };
#005 class pushOnFull { /* ... */ };
#006
#007 void func1() throw (pushOnFull);
#008
#009 void func1() throw (pushOnFull)
#010 {
#011   throw popOnEmpty();  // BCB4 warning: Throw expression violates
#012                        //  exception specification in function
#013                        //  func1() throw(pushOnFull)
#014                        // VC6 : no error, no warning
#015                        // G++ : no error, no warning
#016
#017   throw pushOnFull();  // BCB4 Warning : Unreachable code in function
#018                        // func1() throw(pushOnFull)
#019                        // VC6 : no error, no warning
#020                        // G++ : no error, no warning
#021 }
#022
#023 int main()
#024 {
#025   try {
#026       func1();
#027       return 0;
#028   }
#029   catch ( pushOnFull ) {
#030       cout << "catch pushOnFull" << endl;
#031   }
#032   catch ( popOnEmpty ) {
#033       cout << "catch popOnEmpty" << endl;
#034   }
#035 }
#036 // 執行結果:
#037 // BCB4: Abnormal program termination
#038 // G++ : none(我想是喚起了 C++ standard library function unexpected(),
#039 //       後者喚起 terminate(),其內喚起 abort()。
#040 // VC6 : catch popOnEmpty(我認為 VC6 對於 exception spec. 的處理太粗糙)


■C++ Primer  p643 中
主題:直接在 class 內針對 static const integral data member 給予初值
      (所謂 in-class initialization)
測試結果:VC6[x]  BCB4[o]  G++[o]
實例:

#001 #include <iostream.h>
#002
#003 class Account {
#004 public:
#005   static const int namesize = 16;   // <== in-class initialization
#006 };
#007
#008 const int Account::namesize;
#009
#010 void main()
#011 {
#012   cout << Account::namesize << endl;
#013 }


■C++ Primer p834
主題:class templates 內的 friend function
測試結果:VC6[x]  BCB4[x]  G++[o]
參考:請見稍後 ■C++ Primer p1090 對於 "VC6 的 friend functions" 的深入說明。
實例:

#001 #include <iostream>
#002 using namespace std;
#003
#004 // 以下的 forward declaration 非常重要,見 p834 L-8
#005 template <typename T> class A;
#006 template <typename T> ostream& operator<< (ostream& os, const A<T>& a);
#007
#008 template <typename T> class B;
#009 template <typename T> ostream& operator<< (ostream& os, const B<T>& b);
#010
#011 // 以下以 class A 和 class B 模擬 class Queue 和 class QueueItem 之間的關係
#012
#013 template <typename T>
#014 class A
#015 {
#016   // 以下的 <T> 非常重要,見 p834 L-6
#017   friend ostream& operator<< <T>(ostream& os, const A<T>& a);
#018 public:
#019   A (T i) : _i(i) {
#020             front = new B<T>(i);
#021             back  = new B<T>(i);
#022   }
#023   // 為求完整,應再設計 dtor 以避免 memory leak.
#024
#025 private:
#026   T _i;
#027   B<T>* front;
#028   B<T>* back;
#029 };
#030
#031 template <typename T>
#032 ostream& operator<< (ostream& os, const A<T>& a)
#033 {
#034   os << '<' ;
#035   os << *(a.front) << ' ';
#036   os << *(a.back) << ' ' ;
#037   os << '>' << endl;
#038   return os;
#039 }
#040
#041 template <typename T>
#042 class B
#043 {
#044   friend ostream& operator<< <T>(ostream& os, const B<T>& b);
#045 public:
#046   B (T i) : _item(i) { }
#047 private:
#048   T _item;
#049 };
#050
#051 template <typename T>
#052 ostream& operator<< (ostream& os, const B<T>& b)
#053 {
#054   os << b._item;  // BCB4 error: _item is not accessible. why?
#055   return os;
#056 }
#057
#058 void main()
#059 {
#060   A<int>   a1(5);
#061   A<float> a2(5.4);
#062   A<char>  a3('a');
#063
#064   cout << a1 << a2 << a3 << endl;
#065   /* output :
#066      <5 5 >
#067      <5.4 5.4 >
#068      <a a >
#069   */
#070 }


■C++ Primer p842
主題:class templates(內含 nest types)的 friend functions。
測試結果:VC6[x]  BCB4[x]  G++[o]
實例:

#001 #include <iostream>
#002 using namespace std;
#003
#004 // 以下的 forward declaration 非常重要,見 p834 L-8
#005 template <typename T> class A;
#006 template <typename T> ostream& operator<< (ostream& os, const A<T>& a);
#007
#008 // 以下以 class A 和 class B 模擬 class Queue 和 class QueueItem 之間的關係
#009
#010 template <typename T>
#011 class A
#012 {
#013   // 以下的 <T> 非常重要,見 p834 L-6
#014   friend ostream& operator<< <T>(ostream& os, const A<T>& a);
#015
#016 private:
#017   class B   // nested class
#018   {
#019   public:
#020     B (T i) : _item(i) { }
#021     T _item;
#022   };
#023
#024 public:
#025   A (T i) : _i(i) {
#026             front = new B<T>(i);
#027             back  = new B<T>(i);
#028   }
#029   // 為求完整,應再設計 dtor 以避免 memory leak.
#030
#031 private:
#032   T _i;
#033   B<T>* front;
#034   B<T>* back;
#035 };
#036
#037 template <typename T>
#038 ostream& operator<< (ostream& os, const A<T>& a)
#039 {
#040   os << '<' ;
#041   os << (a.front)->_item << ' ';
#042   os << (a.back)->_item  << ' ' ;
#043   os << '>' << endl;
#044
#045   return os;
#046 }
#047
#048 void main()
#049 {
#050   A<int>   a1(5);
#051   A<float> a2(5.4);
#052   A<char>  a3('a');
#053
#054   cout << a1 << a2 << a3 << endl;
#055   /* output :
#056      <5 5 >
#057      <5.4 5.4 >
#058      <a a >
#059   */
#060 }


■C++ Primer p844
主題:member templates
測試結果:VC6[o]  BCB4[o]  G++[o]
實例:

#001 #include <iostream>
#002 #include <string>
#003 #include <vector>
#004 using namespace std;
#005
#006 template <class T>
#007 class Queue {
#008 public:
#009         // class member template
#010         template <class Type>
#011         class CL
#012         {
#013            Type member;
#014            T mem;
#015         };
#016 public:
#017         // function member template
#018         template <class Iter>
#019         void assign( Iter first, Iter last )
#020         {
#021           cout << "Queue<T>::assign()" << endl;
#022         }
#023 };
#024
#025 void main()
#026 {
#027   // nested types
#028   Queue<int>::CL<char> c;
#029   Queue<int>::CL<string> s;
#030
#031   // instantiation of Queue<int>
#032   Queue<int> qi;
#033
#034   // instantiation of Queue<int>::assign( int *, int * )
#035   int ai[4] = { 0, 3, 6, 9 };
#036   qi.assign(ai, ai+4);         // output: Queue<T>::assign()
#037
#038   // instantiation of Queue<int>::assign( vector<int>::iterator,
#039   //                                      vector<int>::iterator)
#040   vector<int> vi(ai, ai+4);
#041   qi.assign(vi.begin(), vi.end());  // output: Queue<T>::assign()
#042 }


■C++ Primer p853
主題:separate compilation model for class template
推論:既然 BCB4 and G++ and VC6 都未能支援 separate compilation model for
      function templates,我想它們也一定都沒有支援 separate compilation model
      for class templates。但我未做測試(挺煩人 :))

■C++ Primer p856
主題:class template specializations
測試結果:VC6[o]  BCB4[o]  G++[o]


■C++ Primer p861
主題:class template partial specializations
測試結果:VC6[x]  BCB4[o]  G++[o]
實例:

#001 #include <iostream>
#002 using namespace std;
#003
#004 // form 1
#005 template <class T, int hi, int wid>
#006 class Screen {
#007 public:
#008   void print() { cout << hi << ' ' << wid << " form1" << endl; }
#009 };
#010
#011 // form 2 (template partial specialization)
#012 template <class T, int hi>
#013 class Screen <T, hi, 80> {
#014 public:
#015   void print() { cout << hi << " form2" << endl; }
#016 };  // VC6 error C2989
#017
#018 // form 3
#019 template <class T, int hi>
#020 class Screen <T*, hi, 25> {
#021 public:
#022   void print() { cout << hi << ' ' << sizeof(T*) << ' '
#023                       << sizeof(T) << " form3" << endl; }
#024 };
#025
#026 int main()
#027 {
#028   Screen<int, 100, 40>  s1;
#029   Screen<int, 100, 80>  s2;
#030   Screen<int, 500, 25>  s3;
#031   Screen<char*, 300, 25> s4;
#032   Screen<double*, 400, 25> s5;
#033
#034   s1.print(); // output: 100 40 form1
#035   s2.print(); // output: 100 form2
#036   s3.print(); // output: 500 25 form1
#037   s4.print(); // output: 300 4 1 form3
#038   s5.print(); // output: 400 4 8 form3
#039   return 0;
#040 }
您需要登錄後才可以回帖 登錄 | 註冊

本版積分規則

本論壇為非營利之網路平台,所有文章內容均為網友自行發表,不代表論壇立場!若涉及侵權、違法等情事,請告知版主處理。


Page Rank Check

廣告刊登  |   交換連結  |   贊助我們  |   服務條款  |   免責聲明  |   客服中心  |   中央分站

手機版|中央論壇

GMT+8, 2026-8-4 11:01 , Processed in 0.038787 second(s), 16 queries .

Powered by Discuz!

© 2005-2015 Copyrights. Set by YIDAS

快速回復 返回頂部 返回列表