home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #18 / NN_1992_18.iso / spool / comp / lang / cplus / 12772 < prev    next >
Encoding:
Text File  |  1992-08-22  |  1.4 KB  |  58 lines

  1. Xref: sparky comp.lang.c++:12772 comp.std.c++:1106
  2. Path: sparky!uunet!wupost!sdd.hp.com!mips!mips!munnari.oz.au!goanna!gtj
  3. From: gtj@goanna.cs.rmit.oz.au (Glenn T Jayaputera)
  4. Newsgroups: comp.lang.c++,comp.std.c++
  5. Subject: Need help on Template
  6. Message-ID: <14145@goanna.cs.rmit.oz.au>
  7. Date: 23 Aug 92 11:11:51 GMT
  8. Followup-To: poster
  9. Organization: RMIT Department of Computer Science
  10. Lines: 46
  11.  
  12. I need help....
  13. I would like to create a class template that can print it content.
  14. since the value contained can be anything (user define or base types), then I
  15. limit the user that he has to defined his own print routine.  This print routine
  16. is in charge on how to print the object.  This print routine will be pass
  17. to the class template when the user wants to print the value of the
  18. class
  19.  
  20. In short the class template is like this
  21.  
  22. template <class T>
  23. class someClass {
  24.    T *pVal;
  25. public:
  26.   void printObj(void (*CustomPrint)(T *))  {
  27.     CustomPrint(pVal);
  28.   }
  29. };
  30.  
  31. class test  {
  32.   int value;
  33. public:
  34.   friend void printMe(test *);
  35. };
  36.  
  37. void printMe(test *ptest) {
  38.   cout << ptest->value;
  39. }
  40.  
  41. main()  {
  42.   someClass<test> classObj;
  43.   classObj.printObj(printMe);
  44.  
  45.   return 1;
  46. }
  47.  
  48. However, the major drawback of this style is "printMe() should have to be" 
  49. nobody's possesion.  That is I have to make is _friend_ to my type (ie
  50. test).  What I want is the printMe() is the member of class test itself.
  51.  
  52. Can somebody show me how to do it??
  53.  
  54. Thank in advance,
  55. Glenn Jayaputera
  56. no one's o
  57.  
  58.