Friending an inner class to a templated class

Discussion in 'C++' started by mrstephengross, Nov 7, 2006.

  1. 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 ()
     
    mrstephengross, Nov 7, 2006
    #1
    1. Advertising

  2. mrstephengross wrote:
    > 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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, Nov 7, 2006
    #2
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Tomas Ukkonen
    Replies:
    4
    Views:
    481
    Tomas Ukkonen
    May 7, 2004
  2. Pyenos
    Replies:
    2
    Views:
    421
    Pyenos
    Dec 27, 2006
  3. chhenning
    Replies:
    5
    Views:
    387
    chhenning
    Feb 13, 2008
  4. jrwats
    Replies:
    0
    Views:
    333
    jrwats
    Feb 25, 2009
  5. jrwats
    Replies:
    0
    Views:
    388
    jrwats
    Feb 25, 2009
Loading...

Share This Page