home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / cplus / 12902 < prev    next >
Encoding:
Text File  |  1992-08-25  |  2.7 KB  |  108 lines

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!sun-barr!cs.utexas.edu!asuvax!ennews!enuxha.eas.asu.edu!nwatson
  3. From: nwatson@enuxha.eas.asu.edu (Nathan F. Watson)
  4. Subject: Templates, classes, and enumeration types
  5. Message-ID: <1992Aug26.071212.3003@ennews.eas.asu.edu>
  6. Sender: news@ennews.eas.asu.edu (USENET News System)
  7. Organization: Arizona State University
  8. Date: Wed, 26 Aug 1992 07:12:12 GMT
  9. Lines: 97
  10.  
  11. I am trying to write a member function of a class A<T> that will take
  12. an enumerated type argument.  The enumerated type is defined within
  13. A<T> itself.  The code is presented after the discussion.
  14.  
  15. The question is:  how should I specify the enumerated type?  The inline
  16. version (defined within class definition) of the function works fine
  17. in BC++3.1:
  18.  
  19.    template <class T> class A
  20.    {
  21.       public:
  22.          enum Aenum { ... };
  23.          void Afunc(Aenum v) { ... }
  24.    }
  25.  
  26. But no combination I have tried so far works outside the class definition.
  27. The best I can come up with is:
  28.  
  29.    template <class T> void A<T>::Afunc(A<T>::Aenum v) { ... }
  30.  
  31. How should I do this?  Is this a BC++3.1 bug?  Thank you from
  32. nwatson@enuxha.eas.asu.edu.
  33.  
  34. Here is the code:
  35.  
  36. // ---------- try.cpp (BEGIN CODE)
  37.  
  38. #include <iostream.h>
  39.  
  40. #define DO_A 1              // Try class A<T>.
  41. #define DO_B 1              // Try class B (without templates, for comparison).
  42. #define FUNC_INLINE 1       // Inline vs. outline functions.
  43.  
  44. #if DO_A
  45. template <class T>
  46. class A {                   // Class A<T>.
  47.    public:
  48.       enum Aenum { enum0 = 0, enum1 = 1, enum5 = 5 };
  49.       #if FUNC_INLINE
  50.      void Afunc(Aenum v)
  51.         {  cout << (int) v << endl; }
  52.       #else
  53.      void Afunc(Aenum v);
  54.       #endif
  55. };
  56. #if !FUNC_INLINE
  57. template <class T> void A<T>::Afunc(A<T>::Aenum v)
  58. {
  59.    cout << (int) v << endl;
  60. }
  61. #endif
  62. #endif
  63.  
  64. #if DO_B
  65. class B                          // Class B -- works fine in all cases.
  66. {                                // Presented for comparison.
  67.    public:
  68.       enum Benum { enum0 = 0, enum1 = 1, enum5 = 5 };
  69.       #if FUNC_INLINE
  70.      void Bfunc(Benum v)
  71.         { cout << (int) v << endl; }
  72.       #else
  73.      void Bfunc(Benum v);
  74.       #endif
  75. };
  76. #if !FUNC_INLINE
  77. void B::Bfunc(B::Benum v)
  78. {
  79.    cout << (int) v << endl;
  80. }
  81. #endif
  82. #endif
  83.  
  84. int main()
  85. {
  86.    #if DO_A                      // Try A<T>.
  87.    cout << "Doing A" << endl;
  88.    A<int> a;
  89.    a.Afunc(A<int>::enum0);
  90.    a.Afunc(A<int>::enum1);
  91.    a.Afunc(A<int>::enum5);
  92.    #endif
  93.    #if DO_B
  94.    cout << "Doing B" << endl;    // Try B.
  95.    B b;
  96.    b.Bfunc(B::enum0);
  97.    b.Bfunc(B::enum1);
  98.    b.Bfunc(B::enum5);
  99.    #endif
  100.    return 0;
  101. }
  102.  
  103.  
  104. --
  105. ---------------------------------------------------------------------
  106. Nathan F. Watson                             Arizona State University
  107. nwatson@enuxha.eas.asu.edu                Computer Science Department
  108.