home *** CD-ROM | disk | FTP | other *** search
- Newsgroups: comp.lang.c++
- Path: sparky!uunet!haven.umd.edu!darwin.sura.net!mips!decwrl!pa.dec.com!brister
- From: brister@decwrl.dec.com (James Brister)
- Subject: Templates and T::*
- Message-ID: <BRISTER.92Aug26120927@tirade.decwrl.dec.com>
- Sender: news@PA.dec.com (News)
- Organization: DEC Western Software Lab
- Date: 26 Aug 92 12:09:27
- Lines: 78
-
- I'm trying to create a template class that will have a member that is a
- pointer to a member function in the class it is parameterizing. I can get
- this to work in a non template environment just fine, but under templates
- it's causing me grief. Am I doing something wrong? Or is it compiler bug?
-
- Code follows.
-
- Thanks
-
- James
-
- This one works fine. ----------------------------------------------------------
-
- class A {
- public:
- int a ;
- int operator< (const A &rightOper) { return a < rightOper.a ; }
- };
-
- class Foo
- {
- private:
- int (A::* compare) (const A &) ;
-
- public:
- Foo (int a, int (A::* func) (const A &) = 0) ;
- };
-
- Foo::Foo (int a, int (A::* func) (const A &)) {
- if ( func == 0)
- compare = &A::operator< ;
- else
- compare = func ;
- }
-
- main (int argc, char **argv)
- {
- Foo foo (10,&A::operator<) ;
- }
-
-
- This one fails -----------------------------------------------------------------
-
- class A {
- public:
- int a ;
- int operator< (const A & rightOper) { return a < rightOper.a ; }
- };
-
-
- template<class T>
- class Foo
- {
- private:
- int (T::* compare) (const T &) ; // error is: Missing ")".
-
- public:
- Foo (int a, int (T::* func) (const T &) = 0) ;// error: Invalid declarator
- };
-
- template<class T>
- Foo::Foo (int a, int (T::* func) (const T &)) { // error: Parameter has no type or storage class.
- if ( func == 0)
- compare = &T::operator< ; // error: missing ";"
- else // error: invalid statement
- compare = func ;
- }
-
- main (int argc, char **argv)
- {
- Foo<A> foo (10,&A::operator<) ;
- }
-
- ------------------------------------------------------------------------------
-
- --
- James Brister brister@wsl.pa.dec.com
- DEC Western Software Lab., Palo Alto, CA decwrl!brister
-