Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

How to make a template class friend of another class.

0 views
Skip to first unread message

Wiljo

unread,
Feb 24, 2005, 4:24:04 AM2/24/05
to
Hi all,

I have a class with protected and/or private members that need to be accessed by
a close relative but template class. Normally one would make the second class
friend of the first, but when this second class is a template, how can I
accomplish it then?

//#
//# class A
//#
class A
{
public:

friend class B; //* This wont work, because class B is template

private:
void privateFunction();
};
//#
//# template class B
//#
template <class XType>
class B
{
public:
void myFunction( A *ptrA )
{ ptrA->privateFunction(); }
};
//#
//#

Any ideas would be greatly appreciated.

Wiljo.

Thomas Maeder [TeamB]

unread,
Feb 24, 2005, 12:52:55 PM2/24/05
to
Wiljo <inv...@invalid.address> writes:

> I have a class with protected and/or private members that need to be
> accessed by a close relative but template class. Normally one would
> make the second class friend of the first, but when this second class
> is a template, how can I accomplish it then?

Do you want all specializations of B to be friends of A, or just one
particular specialization?


> //#
> //# class A
> //#
> class A
> {
> public:
>
> friend class B; //* This wont work, because class B is template
>
> private:
> void privateFunction();
> };
> //#
> //# template class B
> //#
> template <class XType>
> class B
> {
> public:
> void myFunction( A *ptrA )
> { ptrA->privateFunction(); }
> };
> //#
> //#

What's the point in making B a template?

Wiljo

unread,
Mar 7, 2005, 5:51:59 AM3/7/05
to
Thomas Maeder [TeamB] wrote:

> Do you want all specializations of B to be friends of A, or just one
> particular specialization?
>

Yes, I want all specializations of B to be friend of A.


>
> What's the point in making B a template?

Well I oversimplified the example code a bit too much, and I didn't make sense
anymore. B needs to be template, because it handles several different object
types. The one thing they have in common is that they are all derived from class
A. A has private functionality, that I don't wan't to access directly from any
derived class, hence the private specifier. I DO wan't to access these private
functionality from the template class though. Hence my question.

I solved the problem by making a base class for the template class B. This base
class can be friend of A without any problem.

Thanks anyway.

Wiljo.

Thomas Maeder [TeamB]

unread,
Mar 7, 2005, 11:35:17 AM3/7/05
to
Wiljo <inv...@invalid.address> writes:

This is the wrong newsgroup for this topic.

Cf. my answer over in .cppbuilder.language

0 new messages