tunnuz
tunnuz

Reputation: 23988

Friend class and all its descendants

suppose that I have a class A with several subclasses (B, C, and D). I need B C and D to access some protected members from a class E. Is it possible to make B, C and D friends of E in a single hit without having to list them all?

I have tried with:

class E {

    friend class A;

    ...

};

But this doesn't work.

Thank you

Upvotes: 4

Views: 1341

Answers (1)

camh
camh

Reputation: 42478

You can put protected accessor functions in A, and have A be a friend of E. That way, all derived classes of A can access the members of E via the accessor functions.

Upvotes: 17

Related Questions