Thread: nested class as friend

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    1

    nested class as friend

    class Outer
    {
    class Inner
    {
    ...
    }
    };

    class Other
    {
    ...
    friend class Outer; // OK
    friend class Outer::Inner; // Wrongo (Inner not visible in Other scope as a type within Outer.)
    };

    How in C++ can class Inner also be made friend of class Other? Perhaps friendship should automatically be extended to nested classes of outer class??

  2. #2
    Registered User
    Join Date
    Dec 2001
    Posts
    108
    Using bcc32 this skeletal code compiled without error..

    Code:
    class Outer {
      class Inner {
        //...
      };
    };
    
    class Other {
      //...
      friend class Outer;
      friend class Outer::Inner;
    };
    Don't understand the problem your having

  3. #3
    Registered User matheo917's Avatar
    Join Date
    Sep 2001
    Posts
    279
    in C++ "friendship" is not shared, just b/c you declare Outer to be a friend of an Inner it doesn't mean that automaticly Inner is a friend of outer, it's a bit confusing and totally different when it comes to real world human friendship....

    what you need to do is: inside the body of Inner declare Outer to it's friend and in the body of Inner declare Outer to be a friend of Inner..

    Remember: "friendship" in C++ is not shared, nor inherited, nor it is nested.....

    hope this helps...

    matheo917

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Friend class?
    By legit in forum C++ Programming
    Replies: 7
    Last Post: 11-25-2008, 12:32 PM
  2. Replies: 3
    Last Post: 10-31-2005, 12:05 PM
  3. Problem constructing a class with a nested struc
    By pliang in forum C++ Programming
    Replies: 3
    Last Post: 04-14-2005, 07:43 PM
  4. Declare a template class as a friend?
    By AH_Tze in forum C++ Programming
    Replies: 11
    Last Post: 05-19-2004, 09:24 PM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2003, 12:26 AM