Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > how to declare friend for template class ?

Reply
Thread Tools

how to declare friend for template class ?

 
 
slocum
Guest
Posts: n/a
 
      04-09-2008
how to declare friend for template class ?

class A
{
public:
.....
private:
.....

friend class B ????????
}

template <class T>
class B
{
}

 
Reply With Quote
 
 
 
 
Martin York
Guest
Posts: n/a
 
      04-09-2008
On Apr 9, 5:39 am, slocum <(E-Mail Removed)> wrote:
> how to declare friend for template class ?
>
> class A
> {
> public:
> .....
> private:
> .....
>
> friend class B ????????
>
> }
>
> template <class T>
> class B
> {
>
> }


You can't.
Though you can defined friends for particular versions of B.

template<typename T>
class B
{
};

class A
{
friend class B<int>;
friend class B<float>;
friend class B<MyType>;
// etc..
};
 
Reply With Quote
 
 
 
 
slocum
Guest
Posts: n/a
 
      04-09-2008
And what about this way ??

template <typename T>
class B
{

};

template <typename T>
class A
{
friend class B<T>;

};

 
Reply With Quote
 
Brian Tyler
Guest
Posts: n/a
 
      04-09-2008
> You can't.
> Though you can defined friends for particular versions of B.
>
> template<typename T>
> class B
> {
> };
>
> class A
> {
> friend class B<int>;
> friend class B<float>;
> friend class B<MyType>;
> // etc..
> };


That is completely untrue


template<class T> class B
{
// Stuff
};

class A
{
template <class> friend class B;
};

makes B a friend irrespective of the type of it's template parameter.

Brian.
 
Reply With Quote
 
Martin York
Guest
Posts: n/a
 
      04-10-2008
On Apr 9, 6:03 am, Brian Tyler <(E-Mail Removed)> wrote:
> > You can't.

> That is completely untrue
>
> class A
> {
> template <class> friend class B;
>
> };
>
> makes B a friend irrespective of the type of it's template parameter.


Live and learn.

 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Declaring a template class with two template params a friend in anon-template class A L C++ 1 08-25-2010 07:25 AM
How to declare a friend function of a template class? Rui Maciel C++ 11 07-13-2010 06:08 PM
Declare a Friend function which is a template member function of adifferent class getrakesh@gmail.com C++ 2 02-26-2008 11:32 AM
How can I declare and define a friend template function in a template class? =?gb2312?B?wfXquw==?= C++ 10 08-01-2007 01:48 AM



Advertisments