home *** CD-ROM | disk | FTP | other *** search
- Xref: sparky comp.lang.c++:12772 comp.std.c++:1106
- Path: sparky!uunet!wupost!sdd.hp.com!mips!mips!munnari.oz.au!goanna!gtj
- From: gtj@goanna.cs.rmit.oz.au (Glenn T Jayaputera)
- Newsgroups: comp.lang.c++,comp.std.c++
- Subject: Need help on Template
- Message-ID: <14145@goanna.cs.rmit.oz.au>
- Date: 23 Aug 92 11:11:51 GMT
- Followup-To: poster
- Organization: RMIT Department of Computer Science
- Lines: 46
-
- I need help....
- I would like to create a class template that can print it content.
- since the value contained can be anything (user define or base types), then I
- limit the user that he has to defined his own print routine. This print routine
- is in charge on how to print the object. This print routine will be pass
- to the class template when the user wants to print the value of the
- class
-
- In short the class template is like this
-
- template <class T>
- class someClass {
- T *pVal;
- public:
- void printObj(void (*CustomPrint)(T *)) {
- CustomPrint(pVal);
- }
- };
-
- class test {
- int value;
- public:
- friend void printMe(test *);
- };
-
- void printMe(test *ptest) {
- cout << ptest->value;
- }
-
- main() {
- someClass<test> classObj;
- classObj.printObj(printMe);
-
- return 1;
- }
-
- However, the major drawback of this style is "printMe() should have to be"
- nobody's possesion. That is I have to make is _friend_ to my type (ie
- test). What I want is the printMe() is the member of class test itself.
-
- Can somebody show me how to do it??
-
- Thank in advance,
- Glenn Jayaputera
- no one's o
-
-