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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!haven.umd.edu!darwin.sura.net!mips!decwrl!pa.dec.com!brister
  3. From: brister@decwrl.dec.com (James Brister)
  4. Subject: Templates and T::*
  5. Message-ID: <BRISTER.92Aug26120927@tirade.decwrl.dec.com>
  6. Sender: news@PA.dec.com (News)
  7. Organization: DEC Western Software Lab
  8. Date: 26 Aug 92 12:09:27
  9. Lines: 78
  10.  
  11. I'm trying to create a template class that will have a member that is a
  12. pointer to a member function in the class it is parameterizing. I can get
  13. this to work in a non template environment just fine, but under templates
  14. it's causing me grief. Am I doing something wrong? Or is it compiler bug?
  15.  
  16. Code follows.
  17.  
  18. Thanks
  19.  
  20. James
  21.  
  22. This one works fine. ----------------------------------------------------------
  23.  
  24. class A {
  25.   public:
  26.     int a ;
  27.     int operator< (const A &rightOper) { return a < rightOper.a ; }
  28. };
  29.  
  30. class Foo 
  31. {
  32.   private:
  33.     int (A::* compare) (const A &) ;
  34.     
  35.   public:
  36.     Foo (int a, int (A::* func) (const A &) = 0) ;
  37. };
  38.  
  39. Foo::Foo (int a, int (A::* func) (const A &)) {
  40.     if ( func == 0)
  41.       compare = &A::operator< ;
  42.     else
  43.       compare = func ;
  44. }
  45.  
  46. main (int argc, char **argv) 
  47. {
  48.     Foo foo (10,&A::operator<) ;
  49. }
  50.  
  51.  
  52. This one fails -----------------------------------------------------------------
  53.  
  54. class A {
  55.   public:
  56.     int a ;
  57.     int operator< (const A & rightOper) { return a < rightOper.a ; }
  58. };
  59.  
  60.  
  61. template<class T>
  62. class Foo 
  63. {
  64.   private:
  65.     int (T::* compare) (const T &) ; // error is: Missing ")".
  66.     
  67.   public:
  68.     Foo (int a, int (T::* func) (const T &) = 0) ;// error: Invalid declarator
  69. };
  70.  
  71. template<class T>
  72. Foo::Foo (int a, int (T::* func) (const T &)) { // error: Parameter has no type or storage class.
  73.     if ( func == 0)
  74.       compare = &T::operator< ; // error: missing ";"
  75.     else                     // error: invalid statement
  76.       compare = func ;
  77. }
  78.  
  79. main (int argc, char **argv) 
  80. {
  81.     Foo<A> foo (10,&A::operator<) ;
  82. }
  83.  
  84. ------------------------------------------------------------------------------
  85.  
  86. --
  87. James Brister                                           brister@wsl.pa.dec.com
  88. DEC Western Software Lab., Palo Alto, CA                        decwrl!brister
  89.