Topic: Templats, friends, an ostreams (Templates, friends and r.14.7)


Author: timd@Starbase.NeoSoft.COM (Two-eyed Tim)
Date: 1995/05/12
Raw View
In article <3p06ld$mb9@bcarh8ab.bnr.ca>,
Vinayak Patilkulkarni <patil@bnr.ca> wrote:
>
>
>Hi,
>
>I have a problem declaring a templated class as friend. C++ programming
>language does not seem to handle it.
>
>Consider the simple example given below.
>
>#include <iostream.h>
>
>class QElem {
>
>private :
>    void stamp () {}
>    friend class Q;
>    // What i intend to do here is to make stamp()
>    // as friend function of all Q<T> classes.

That looks backwards from what you are doing...your code is trying
to make all Q<T> classes a friend of QElem...if that's what you want,
you might try this:

   //make a parent class of Q, say PQ;

   class PQ
   {
      ...
   }

   class QElem
   {
      ...
      void stamp() {}
      friend class PQ;
      ...
   }

   template <class T> class Q : public PQ // inheriting routines that
                                          // can see inside QElem
   {
      ...
   }

>    // ObjectCenter compiler takes the above line and makes
>    // stamp () as friend function of all types of Q.

But that's not what your code said...that would require code
that says:

   template <class T> class Q
   {
      ...
      friend void QElem::stamp();
      ...
   }

May be I misunderstood?

--

I have a similar problem...I want to make a "<<" (output) operator
function for templates...can I do something like:

   template <class T> class X
   {
      ...
      friend ostream & operator << (ostream & s, X<T> x);
      ...
   }

And

   inline ostream & operator << (ostream & s, X<T> x)
   {
      ...
   }

Can I do something like this?

HELP!

-t
--
   "...I'm fixing a hole where the rain gets in...
    and stops my mind from wondering..."