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

  • SEARCH
  • Home
  • 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 7706781
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T00:11:52+00:00 2026-06-01T00:11:52+00:00

Given the sample code: class Base { public: bool pub; protected: bool prot; };

  • 0

Given the sample code:

class Base {
public:
  bool pub;
protected:
  bool prot;
};

class Derived : private Base {
  friend class MyFriend;
};

class MyFriend {
  Derived _derived;

  void test() {
    // Does standard provide me access to _derived.pub and _derived.prot?
    cout << "Am I allowed access to this: " << _derived.pub
         << " and this: " << _derived.prot;
  }
};

Does being a friend give me all access I would get as if I was a member function within the class to which I am a friend? In other words, can I get at the protected and public members of the base class which is privately inherited from since I am a friend?

  • 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-01T00:11:53+00:00Added an answer on June 1, 2026 at 12:11 am

    Combining the answers of David Rodríguez – dribeas and Luchian Grigore:

    Yes, the example in the question works, however, as David points out, the protected members are not accessible directly through the base class. You only get access to the protected members when accessed through Derived, you do not have access to the same members when accessed through Base.

    In other words, the protected members of base are treated as if they were private members of derived and thus friends can see them, but, if you cast to the base class, there is no friend relationship, and thus the protected members are no longer accessible.

    Here is an example that clarifies the difference:

    class MyFriend {
      Derived _derived;
    
      void test() {
        bool thisWorks = _derived.pub;
        bool thisAlsoWorks = _derived.prot;
    
        Base &castToBase = _derived;
    
        bool onlyPublicAccessNow = castToBase.pub;
        // Compiler error on next expression only.
        // test.cpp:13: error: `bool Base::prot' is protected
        bool noAccessToProtected = castToBase.prot;
      }
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given this sample code: #include <iostream> #include <stdexcept> class my_exception_t : std::exception { public:
Consider the following sample code: #include <iostream> using namespace std; class base { public:
Given the following classes: public class Class1<TObject> { protected void MethodA<TType>(Expression<Func<TObject, TType>> property, ref
Given this code: public class Car { public virtual int CarId { get; set;
Considering the following sample code: // delivery strategies public abstract class DeliveryStrategy { ...
Conside the following sample code below: #include <iostream> using namespace std; class base {
Given the following code sample: uint8_t i, in, ni; i = in = 2;
Given an object like: class M(models.Model): test = models.BooleanField() created_date = models.DateTimeField(auto_now_add=True) Given sample
In the code base I was maintaining I found this exact class, pasted below.
I understand that derived class is type compatible with a pointer to its base

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.