Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Re: Templates and friends and template methods with private methods.

Reply
Thread Tools

Re: Templates and friends and template methods with private methods.

 
 
Buster Copley
Guest
Posts: n/a
 
      07-05-2003
Gianni Mariani wrote:
> I have a 2 co-operating class templates that need to peer into each
> other's privates. Each of these have template methods. How does one
> manage friendness in this scenario ? See code below:
>
> template <typename A>
> class Y;
>
> template <typename A>
> class X
> {
>
> friend class Y<A>;


// The above makes Y <T> a friend of X <T>, for each
// specialization (instantiation) of X <T>. So in
// your main (), the only friendship granted is that
// Y <int> is a friend of X <int>.

// If you make the template class Y a friend of X, as:

template <typename T> class Y;

// it has the effect of making every specialization
// of Y a friend of every specialization of X.
// Plus, it counts as a forward declaration of Y,
// so you get to have two fewer lines of code.

> private:
>
> A m_a;
>
> public:
>
> X( A i_val );
>
> template<typename B> void DoThing( Y<B> & b )
> {
> m_a = b.m_a;
> }
>
> };


I hope this helps. I learned about it in the excellent
chapter on templates in Ray Lischner's "C++ In A Nutshell".

Buster Copley

 
Reply With Quote
 
 
 
 
Buster Copley
Guest
Posts: n/a
 
      07-05-2003
I didn't see John's reply, despite your warning.
I'm getting used to some new news software.

Good luck,
Buster

 
Reply With Quote
 
 
 
 
John Harrison
Guest
Posts: n/a
 
      07-06-2003
>
> Buster, thanks for your reply. However the cigar is still in the box.
>
> Either gcc 3.3 does not accept this or I am doing somthing silly.
>
> This shows up as syntax errors
> template <typename A> class X {
> friend template <typename Z> class Y;
> };
>
>
> This forward declates Y in the scope of class X i.e. X::Y
> template <typename A> class X {
> template <typename Z> class Y;
> };
>
> This does not end up making a friend of anything
> template <typename A> class X {
> template <typename Z> class ::Y;
> };
>
> Note that this is all as per gcc 3.3, it could be a problem with gcc.
>
> The last one looked promising but I get errors regarding accessing
> private members.
>
> The syntax:
> friend template <typename Z> class ::Y;
>
> gives numerous errors.
>


Did you not see my earlier post?

template <typename A>
class Y;

template <typename A>
class X
{
template <typename B>
friend class Y;
};

john


 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      07-06-2003
John Harrison wrote:

>
> Did you not see my earlier post?


Nope - my news server is truly messed up at the moment.

>
> template <typename A>
> class Y;
>
> template <typename A>
> class X
> {
> template <typename B>
> friend class Y;
> };
>


<rant>
Oooh. (Fat lady sings.... Lighting cigar ... )

Darn, I shoulda guessed that one. A friend *template* ... that makes
sense. The deduction is that the C++ semantics of a friend models that
of any member i.e. a "friend" is like an attribute of a member to a class.
</rant>

The code below compiles without a peep with gcc 3.3. MSVC++(SP5)
chunders at least 16 errors. I need to get a copy of a better native MS
win32 compiler.


template <typename A> class X;
template <typename A> class Y;

template <typename A>
class X
{

template <typename Z> friend class Y;
private:

A m_a;

public:
X( A i_val );

template<typename B> void DoThing( Y<B> & b )
{
m_a = b.m_a;
}
};

template <typename A>
class Y
{

template <typename Z> friend class X;
private:

A m_a;

public:
Y( A i_val );

template<typename B> void DoOtherThing( X<B> & b )
{
m_a = b.m_a;
}
};

int main()
{

Y<int> yi( 1 );
Y<short> ys( 1 );
X<short> xs( 2 );

xs.DoThing( ys );
xs.DoThing( yi );

yi.DoOtherThing( xs );
}

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      07-07-2003
Buster Copley wrote:
> I'm glad you've got it sorted out. I did mean to include the word friend
> in there; that's what I had in emacs, but I missed it out when I was
> retyping the message.
>
> The template friend declaration is a forward declaration for the
> template, so you can delete the other forward declarations if you put
> the two classes one way round.
>


Thanks to both John and you for taking the time to answer the question.

 
Reply With Quote
 
Gianni Mariani
Guest
Posts: n/a
 
      07-07-2003
John Harrison wrote:

>
> Did you not see my earlier post?
>


FYI ... I think the problem might be on your end, it's not in Google
either ...

Thx

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Private methods not so private? Frank Meyer Ruby 14 08-02-2007 07:29 PM
Friends don't let friends drink and fly through space =?ISO-8859-1?Q?R=F4g=EAr?= Computer Support 6 07-29-2007 03:52 AM
Why is define_method private? Plus,what's the point of private methods? Daniel Finnie Ruby 3 12-16-2006 10:09 PM
how to Specializations of function Templates or Overloading Function templates with Templates ? recover C++ 2 07-25-2006 02:55 AM
Templates templates templates JKop C++ 3 07-21-2004 11:44 AM



Advertisments