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

  1. Newsgroups: comp.lang.c++
  2. Path: sparky!uunet!mcsun!ieunet!tcdcs!unix1.tcd.ie!ghawkins
  3. From: ghawkins@unix1.tcd.ie (George C. Hawkins)
  4. Subject: How do I write a typedef to go with my template class?
  5. Message-ID: <ghawkins.714856261@unix1.tcd.ie>
  6. Summary: See subject line.
  7. Keywords: template generic define
  8. Sender: usenet@cs.tcd.ie (NN required at ashe.cs.tcd.ie)
  9. Nntp-Posting-Host: unix1.tcd.ie
  10. Organization: Trinity College, Dublin
  11. Date: Wed, 26 Aug 1992 19:11:01 GMT
  12. Lines: 56
  13.  
  14.  
  15. I can use templates for classes but how do I write a typedef to go
  16. with a template class. At the moment I have a horrible solution
  17. using a massive #define. I have a '.h' file, such that if I have
  18. a class X and use the line:
  19.  
  20.     Callbackdeclare(X);
  21.  
  22. I get a new class (called Callback<X> if I was using templates).
  23. *And* I also get a typedef to go with it. I think to reimplement
  24. the class to be a template class is no problem, but how do I get
  25. the very handy typedef to go with it. As far as I can see templates
  26. only have "scope" over the class or whatever that follows them. I.e.
  27. I can't do:
  28.  
  29.     template <class X>
  30.     class Callback {
  31.         ...
  32.     }
  33.     typedef void (X::func_ptr)(int);
  34.  
  35. And even if I could I'd get name clashes for all my typedefs for
  36. Callback class being used with different X types.
  37.  
  38. So what's the solution? At the moment I have this code to
  39. do things:
  40.  
  41. #define Callbackdeclare(CLASS)                    \
  42. class CLASS;                            \
  43.                                 \
  44. typedef void (CLASS::*name2(CLASS,PMF_CB))( int );        \
  45.                                 \
  46. class name2(CLASS,Callback) {                    \
  47.                                 \
  48. private:                            \
  49.                                 \
  50.     CLASS* _obj;                        \
  51.     name2(CLASS,PMF_CB) _pmf;                \
  52.     ...
  53.  
  54. Fairly ghastly stuff I guess, but it works and solves a really
  55. tricky problem.  (If you haven't come across name2(x,y) before it's
  56. just a concatenation macro, so after cpp has gone through say
  57. 'name2(call,back)' it will be replaced by 'callback'.)
  58.  
  59. So please, please help me develop a clean solution with templates.
  60. Thanks for your time, yours,
  61.  
  62.  
  63. George.
  64.  
  65. --
  66. george lives at:
  67. ___________________________________________________________________________
  68. |   ghawkins@unix1.tcd.ie (mostly)  |   ghawkins@vax1.tcd.ie (sometimes)  |
  69. ---------------------------------------------------------------------------
  70.