Friending an inner class to a templated class

M

mrstephengross

I'm making progress on mixing templates with friends (sounds like a
drinking game, huh?). Anyway, here's the situation. I've got an "Outer"
class with a private "Inner" class (sub-class, technically). I want to
declaring an unrelated "Thing" class to be a friend of the inner class.
However, since the inner class is private, the compiler complains.
Here's what the code looks like:


====================================================
template<typename T> class Thing;

class Outer {
private:
class Inner {
public: friend class Thing<Outer::Inner>;
private: int x;
};
};

template<> class Thing<Outer::Inner> { static void go() { Outer::Inner
i; } };
======================================================

So even though the friend statement should make the Inner class
accessible to class Thing, the compiler complains that Outer::Inner is
private, and thus inaccessible. Any ideas on how to make it work?

Thanks in advance,
--Steve ([email protected])
 
V

Victor Bazarov

mrstephengross said:
I'm making progress on mixing templates with friends (sounds like a
drinking game, huh?). Anyway, here's the situation. I've got an
"Outer" class with a private "Inner" class (sub-class, technically).
I want to declaring an unrelated "Thing" class to be a friend of the
inner class. However, since the inner class is private, the compiler
complains. Here's what the code looks like:


====================================================
template<typename T> class Thing;

class Outer {
private:
class Inner {
public: friend class Thing<Outer::Inner>;
private: int x;
};
};

template<> class Thing<Outer::Inner> { static void go() { Outer::Inner
i; } };
======================================================

So even though the friend statement should make the Inner class
accessible to class Thing, the compiler complains that Outer::Inner is
private, and thus inaccessible. Any ideas on how to make it work?

To let your 'Thing<Outer::Inner>' access 'Outer::Inner', it has to be
a friend of 'Outer', not of 'Outer::Inner'. You just gave your template
specialisation access to the 'x' variable. You didn't give access to
the 'Inner' type.

V
 

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

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top