Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7851653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T19:13:55+00:00 2026-06-02T19:13:55+00:00

I was looking through some C++ code written by a (now departed) coworker quite

  • 0

I was looking through some C++ code written by a (now departed) coworker quite a long time ago, and found an odd class definition I’m trying to decipher.

class BaseClass
{
    friend SubClass1;
    friend SubClass2;
}

class SubClass1 : public BaseClass
{
    ...
}

class SubClass2 : public BaseClass
{
    ...
}

Is there a benefit to designing a class hierarchy this way? If you want access to private methods of BaseClass from the subclasses wouldn’t you just move them to protected instead of private? I feel there is an idiom I’m missing here.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T19:13:56+00:00Added an answer on June 2, 2026 at 7:13 pm

    It is hard to say without looking at the real design of the library, but the two approaches are not equivalent. Using friend in this way provides greater access to lesser types than you can get by declaring all members protected.

    Greater access

    The meaning of protected is not exactly grant access to all base members everywhere to the derived type, but rather grant access to the protected members of the base subobject inside the derived type. The difference is that a derived type cannot access protected members or a type that is not of its own type or derived types.

    Consider two versions of a class one of which has all members protected and no friend declaration, another that has all members private and declares a subclass as a friend. Now consider that the derived type had a function:

    struct derived : base {
       void f( base& b ) {
           //std::cout << b.protected_method() << std::endl; // Error
           std::cout << protected_method() << std::endl;     // Ok, accessing your own base
       }
    };
    

    In the case of using protected, the issue there is that protected does not let you tweak the contents of any object other than derived or types derived from derived, but the argument could be a base or any other type that extends base and it not otherwise related to derived.

    This restriction is a bit less clear in some other use cases, and you might be able to obtain access to those protected members outside of your own hierarchy, just not in a direct simple way (I consider that a bug in the access specifier specification of the language).

    On the other hand, if derived is a friend of the base the above code will compile, as derived is granted access to every base instance anywhere, be it a subobject of derived or not.

    To lesser types

    The protected access specifier is transtive, once you grant access through protected to a derived type, you are granting it to all types that derive from it, and also to any other type that might inherit directly from you. It is impossible to control what types are granted access and which are not. On the other hand friendship is precise, only the members of the types declared as friend will have access. Friendship is not transitive, so type other than your declared friends will have access.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

So I'm looking through some code, and I see this: class whatever { public:
Looking through some code I came across the following code trTuDocPackTypdBd.update(TrTuDocPackTypeDto.class.cast(packDto)); and I'd like
I was looking through some code and found 2 lines that perplexed me: add
I'm looking through some code for learning purposes. I'm working through this portion of
I have been looking through some code on an open source project recently and
I was looking through some compiled coffee-script code, and I noticed something like the
I'm running some code through FxCop and am currently looking at clearing all of
I'm looking for some code (C# or VB.NET preferred) to iterate through all folders
I was looking through some code for work, and came across this line: eval(\$element
When looking through some code that was handled by another employee, I see a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.