1

I am modifying some C++ code that start with the following code:

class AAAA {
public:
    class BBBB : public CCCC {   // CCCC from include CCCC.h
        friend class AAAA;
        typedef ... ;

    public:
        BBBB() {}
        BBBB(AAAA& thething, uint8_t a = 1) {
           init(&thething, a);
        }
        virtual ~BBBB(){}
//...
}

However as non-professional C++ programmer, this is very confusing and daunting.

Why would a subclass have its super class as a friend class?

What is the meaning of:

(a) class BBBB : public CCCC, and
(b) BBBB() {} followed by
(c) virtual ~BBBB(){}, in this case?


I have already looked at the following SO answers:

not2qubit
  • 14,531
  • 8
  • 95
  • 135
  • 1
    Are you asking about class nesting? C++ allows that, nothing really unusual about it. Why someone used that depends on the concrete case. Maybe it is not a correct architecture. Or maybe it is, we can't possibly know that without seeing the real code. – freakish Jul 25 '19 at 12:14
  • 1
    You posted some code that doesn't relate to the inheritance alone, so I assume it's not just that which you find unclear. Could you please highlight what exactly you want an answer to focus on? – StoryTeller - Unslander Monica Jul 25 '19 at 12:17
  • ^ *That* much I understood myself, and the code works. But I don't understand most other parts. Why would a subclass have its super class as a *friend* class? What is the meaning of (a) `class BBBB : public CCCC`, and (b) `BBBB() {}` followed by (c) `virtual ~BBBB(){}`, in this case? – not2qubit Jul 25 '19 at 12:20
  • ^^ that should be your question instead of "How can I make sense of all this?" which is too broad and unclear – 463035818_is_not_an_ai Jul 25 '19 at 12:21
  • 1
    @not2qubit (a) simple class definition with inheritance, (b) constructor, (c) virtual destructor. There's nothing else here. The question why a nested class has its "owner" class as a friend sounds interesting though. Mistake? Code evolution? – freakish Jul 25 '19 at 12:22

1 Answers1

3

First, I would look at BBBB and CCCC by themselves.

class BBBB : public CCCC {   // CCCC from include CCCC.h
    friend class AAAA;
    typedef ... ;

public:
    BBBB() {}
    BBBB(AAAA& thething, uint8_t a = 1) {
       init(&thething, a);
    }
    virtual ~BBBB(){}
}

BBBB is a pretty standard class that derives from CCCC. The only unusual thing is the friend class AAAA, which allows the methods in AAAA to access private (and protected) methods and fields in BBBB.

The fact that BBBB is nested in AAAA means that to use BBBB, you'd have to access it with AAAA::BBBB b;, and that BBBB can access private (and protected) methods and fields in AAAA.

Why would a subclass have its super class as a friend class?
What is the meaning of:
(a) class BBBB : public CCCC, and
(b) BBBB() {} followed by
(c) virtual ~BBBB(){}, in this case?

AAAA is not the super class of BBBB, it is the enclosing class. See above.

(a) indicates that BBBB inherits from CCCC. (b) is the default constructor. (c) is the destructor, and for classes using inheritance it is recommended to make destructors virtual.

not2qubit
  • 14,531
  • 8
  • 95
  • 135
joshwilsonvu
  • 2,569
  • 9
  • 20