home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!sun-barr!cs.utexas.edu!asuvax!ennews!enuxha.eas.asu.edu!nwatson
- From: nwatson@enuxha.eas.asu.edu (Nathan F. Watson)
- Subject: Templates, classes, and enumeration types
- Message-ID: <1992Aug26.071212.3003@ennews.eas.asu.edu>
- Sender: news@ennews.eas.asu.edu (USENET News System)
- Organization: Arizona State University
- Date: Wed, 26 Aug 1992 07:12:12 GMT
- Lines: 97
-
- I am trying to write a member function of a class A<T> that will take
- an enumerated type argument. The enumerated type is defined within
- A<T> itself. The code is presented after the discussion.
-
- The question is: how should I specify the enumerated type? The inline
- version (defined within class definition) of the function works fine
- in BC++3.1:
-
- template <class T> class A
- {
- public:
- enum Aenum { ... };
- void Afunc(Aenum v) { ... }
- }
-
- But no combination I have tried so far works outside the class definition.
- The best I can come up with is:
-
- template <class T> void A<T>::Afunc(A<T>::Aenum v) { ... }
-
- How should I do this? Is this a BC++3.1 bug? Thank you from
- nwatson@enuxha.eas.asu.edu.
-
- Here is the code:
-
- // ---------- try.cpp (BEGIN CODE)
-
- #include <iostream.h>
-
- #define DO_A 1 // Try class A<T>.
- #define DO_B 1 // Try class B (without templates, for comparison).
- #define FUNC_INLINE 1 // Inline vs. outline functions.
-
- #if DO_A
- template <class T>
- class A { // Class A<T>.
- public:
- enum Aenum { enum0 = 0, enum1 = 1, enum5 = 5 };
- #if FUNC_INLINE
- void Afunc(Aenum v)
- { cout << (int) v << endl; }
- #else
- void Afunc(Aenum v);
- #endif
- };
- #if !FUNC_INLINE
- template <class T> void A<T>::Afunc(A<T>::Aenum v)
- {
- cout << (int) v << endl;
- }
- #endif
- #endif
-
- #if DO_B
- class B // Class B -- works fine in all cases.
- { // Presented for comparison.
- public:
- enum Benum { enum0 = 0, enum1 = 1, enum5 = 5 };
- #if FUNC_INLINE
- void Bfunc(Benum v)
- { cout << (int) v << endl; }
- #else
- void Bfunc(Benum v);
- #endif
- };
- #if !FUNC_INLINE
- void B::Bfunc(B::Benum v)
- {
- cout << (int) v << endl;
- }
- #endif
- #endif
-
- int main()
- {
- #if DO_A // Try A<T>.
- cout << "Doing A" << endl;
- A<int> a;
- a.Afunc(A<int>::enum0);
- a.Afunc(A<int>::enum1);
- a.Afunc(A<int>::enum5);
- #endif
- #if DO_B
- cout << "Doing B" << endl; // Try B.
- B b;
- b.Bfunc(B::enum0);
- b.Bfunc(B::enum1);
- b.Bfunc(B::enum5);
- #endif
- return 0;
- }
-
-
- --
- ---------------------------------------------------------------------
- Nathan F. Watson Arizona State University
- nwatson@enuxha.eas.asu.edu Computer Science Department
-