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 8295631
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T14:40:36+00:00 2026-06-08T14:40:36+00:00

Why does this compile: class FooBase { protected: void fooBase(void); }; class Foo :

  • 0

Why does this compile:

class FooBase
{
protected:
    void fooBase(void);
};

class Foo : public FooBase
{
public:
    void foo(Foo& fooBar)
    {
        fooBar.fooBase();
    }
};

but this does not?

class FooBase
{
protected:
    void fooBase(void);
};

class Foo : public FooBase
{
public:
    void foo(FooBase& fooBar)
    {
        fooBar.fooBase();
    }
};

On the one hand C++ grants access to private/protected members for all instances of that class, but on the other hand it does not grant access to protected members of a base class for all instances of a subclass.
This looks rather inconsistent to me.

I have tested compiling with VC++ and with ideone.com and both compile the first but not the second code snippet.

  • 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-08T14:40:38+00:00Added an answer on June 8, 2026 at 2:40 pm

    When foo receives a FooBase reference, the compiler doesn’t know whether the argument is a descendant of Foo, so it has to assume it’s not. Foo has access to inherited protected members of other Foo objects, not all other sibling classes.

    Consider this code:

    class FooSibling: public FooBase { };
    
    FooSibling sib;
    Foo f;
    f.foo(sib); // calls sib.fooBase()!?
    

    If Foo::foo can call protected members of arbitrary FooBase descendants, then it can call the protected method of FooSibling, which has no direct relationship to Foo. That’s not how protected access is supposed to work.

    If Foo needs access to protected members of all FooBase objects, not just those that are also known to be Foo descendants, then Foo needs to be a friend of FooBase:

    class FooBase
    {
    protected:
      void fooBase(void);
      friend class Foo;
    };
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This program does not compile: public class xx { static class Class1<C> { void
Does anyone know why this doesn't compile? public class ArrayCompare{ public static void main
Why does this not compile? File.hpp class CTest { public: enum enumTest { EN_TEST
This does not compile in C++: class A { }; class B : public
Why this code does not compile (Cygwin)? #include <vector> template <class Ttile> class Tilemap
I have this class/interface definitions in C# public class FooBase { ... protected bool
Suppose I have this class hierarchy: class A { public: virtual void foo(Base *b)
Why does this compile in java 6 (Sun 1.6.0_16): System.out.println(\u000B); ... but not this:
This doesn't compile, #include <boost/intrusive_ptr.hpp> class X { public: void intrusive_ptr_add_ref(X* blah) { }
Why does this code not compile? (gcc 4.7.0) // Class with a simple getter/setter

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.