Friend cannot inherit from private subclass

This is a discussion on Friend cannot inherit from private subclass within the C++ Programming forums, part of the General Programming Boards category; I am trying to inherit a subclass, which is declared as a private member of another class. Although the Friend ...

  1. #1
    Registered User
    Join Date
    Oct 2006
    Posts
    250

    Friend cannot inherit from private subclass

    I am trying to inherit a subclass, which is declared as a private member of another class. Although the Friend is declared as a friend of Class, my compiler spits out the following error:
    Class.h:5: error: ‘class Class::SubClass’ is private
    Friend.h:3: error: within this context
    Could anyone explain to me what I am getting wrong here? BTW, the compiler I am using is gcc 4.1.2.

    Class.h, the class with the private subclass that I am trying to inherit:

    Code:
    class Class
    {
        private:
            class SubClass
            {
                public:
                    void A();
            };
        public:
            friend class Friend;
    };
    Friend.h, the class that is trying to inherit the private subclass:

    Code:
    #include "Class.h"
    
    class Friend: public Class::SubClass
    {
    };

  2. #2
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    You are trying to inherit from a privately accessible class. The error really is just that. No matter what you do with friendship to try to go around this rule, the sad truth is that you can't.

    The derivation list is in the same scope as of the class Friend definition (from what is visible from your code, the global scope). SubClass is private to Class. You can't "see" it.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Posts
    250
    But a declaring a certain class as friend automatically makes all private entries visible to that class (i.e. as if they were public to that class). According to this logic this should make the class inheritable by friend.

  4. #4
    (?<!re)tired Mario F.'s Avatar
    Join Date
    May 2006
    Location
    Portugal
    Posts
    7,412
    Lets look at the first line of your Friend class definition.

    Code:
    class Friend: public Class::SubClass
    A class definition starts with the keyword class followed by the name of the class. This is not new to you. But what about what's comming next?

    Everything starting with (and including) the colon is called a derivation list. And you should look at it separately from your class. The derivation list needs to fully qualify the class that will be inherited.

    Code:
    : public Class::SubClass
    SubClass is indeed in the scope of Class (because classes also define a scope). But it is a private member of Class. It is not visible to the derivation list. In other words, Class doesn't expose SubClass as part of its interface.

    You cannot derive from a private nested class.
    The programmer’s wife tells him: “Run to the store and pick up a loaf of bread. If they have eggs, get a dozen.”
    The programmer comes home with 12 loaves of bread.


    Originally Posted by brewbuck:
    Reimplementing a large system in another language to get a 25% performance boost is nonsense. It would be cheaper to just get a computer which is 25% faster.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Still a rather vague explanation.

    In short, the base-clause (the standard's name for the derivation list) is not part of the class body, and it's only the class body that gains access to private and protected members.
    See 11.4 &#167;2 of the C++ standard. It explicitely mentions this particular case and provides this example:
    Code:
             class A {
                      class B { };
                      friend class X;
              };
              class X : A::B {         // ill-formed: A::B cannot be accessed
                                       //  in the base-clause for X
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# and SQL
    By siten0308 in forum C# Programming
    Replies: 2
    Last Post: 07-09-2008, 12:34 PM
  2. Replies: 12
    Last Post: 01-09-2007, 03:26 PM
  3. webBrowser problem
    By algi in forum C# Programming
    Replies: 7
    Last Post: 08-19-2005, 09:35 AM
  4. Need help with calculator program
    By Kate in forum C# Programming
    Replies: 1
    Last Post: 01-16-2004, 09:48 AM
  5. Quack! It doesn't work! >.<
    By *Michelle* in forum C++ Programming
    Replies: 8
    Last Post: 03-01-2003, 11:26 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21