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:

5
  • 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. Commented Jul 25, 2019 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? Commented Jul 25, 2019 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? Commented Jul 25, 2019 at 12:20
  • ^^ that should be your question instead of "How can I make sense of all this?" which is too broad and unclear Commented Jul 25, 2019 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? Commented Jul 25, 2019 at 12:22

1 Answer 1

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.