Making a template class a friend

J

Juha Nieminen

Suppose I have something along the lines of:

class A
{
void somePrivateFunction();
};

template<typename T>
class B
{
public:
void foo(A* a)
{
a->somePrivateFunction();
}
};

I would like to say inside A that *any* instantiation of B (regardless
of the template parameter T) is a friend. I would like to do so without
making A a template class.

Is this impossible?
 
M

mlimber

  Suppose I have something along the lines of:

class A
{
    void somePrivateFunction();

};

template<typename T>
class B
{
 public:
    void foo(A* a)
    {
        a->somePrivateFunction();
    }

};

  I would like to say inside A that *any* instantiation of B (regardless
of the template parameter T) is a friend. I would like to do so without
making A a template class.

  Is this impossible?

No. Add to A:

template<class> friend class B;

Cheers! --M
 
L

Lionel B

[...]
  I would like to say inside A that *any* instantiation of B
  (regardless
of the template parameter T) is a friend. I would like to do so without
making A a template class.

  Is this impossible?

No. Add to A:

template<class> friend class B;

Wow, wasn't aware of that syntax. That's really useful.

Cheers,
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,596
Members
45,130
Latest member
MitchellTe
Top