Topic: Need help on Template


Author: gtj@goanna.cs.rmit.oz.au (Glenn T Jayaputera)
Date: 23 Aug 92 11:11:51 GMT
Raw View
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