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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T04:06:46+00:00 2026-05-29T04:06:46+00:00

From http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.5 A member (either data member or member function) declared in a protected

  • 0

From http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.5

A member (either data member or member function) declared in a protected section of a class can only be accessed by member functions and friends of that class, and by member functions and friends of derived classes

So, what is the way to access the protected function fun in the derived class?

#include <iostream>
using namespace std;

class X
{
    private:
        int var;
    protected:
        void fun () 
        {
            var = 10;
            cout << "\nFrom X" << var; 
        }
};

class Y : public X
{
    private:
        int var;
    public:
        void fun () 
        {
            var = 20;
            cout << "\nFrom Y" << var;
        }

        void call ()
        {
            fun ();

            X objX;
            objX.fun ();
        }
};

This results in:

anisha@linux-dopx:~/> g++ type.cpp
type.cpp: In member function ‘void Y::call()’:
type.cpp:9:8: error: ‘void X::fun()’ is protected
type.cpp:32:14: error: within this context

I saw this: Accessing protected members in a derived class

Given:

You can only access protected members in instances of your type (or
derived from your type). You cannot access protected members of an
instance of a parent or cousin type.

In your case, the Derived class can only access the b member of a
Derived instance, not of a different Base instance.

Changing the constructor to take a Derived instance will also solve
the problem.

How can this be accomplished without changing the constructor declaration?

  • 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-05-29T04:06:46+00:00Added an answer on May 29, 2026 at 4:06 am

    I think that the thing you are trying to do should looks like this:

    #include <iostream>
    using namespace std;
    
    class X
    {
        private:
            int var;
    protected:
        virtual void fun () 
        {
            var = 10;
            cout << "\nFrom X" << var; 
        }
    };
    
    class Y : public X
    {
    private:
        int var;
    public:
        virtual void fun () 
        {
            var = 20;
            cout << "\nFrom Y" << var;
        }
    
        void call ()
        {
            fun ();
    
    
            X::fun ();
        }
    };
    

    That way you can invoke hiden member from your base class. Otherwise you have to add friend X as it was pointed in other post.

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

Sidebar

Related Questions

From: http://www.parashift.com/c++-faq-lite/basics-of-inheritance.html#faq-19.9 Three keys: ROI, ROI and ROI. Every interface you build has a
From c++ FAQ: http://www.parashift.com/c++-faq-lite/dtors.html#faq-11.9 Remember: delete p does two things: it calls the destructor
The question is from C++ faq. http://www.parashift.com/c++-faq-lite/protected-virtuals.html Code using public overloaded virtuals: class Base
I have a question based on this question In the section http://www.parashift.com/c%2B%2B-faq-lite/private-inheritance.html#faq-24.3 the following
From http://www.parashift.com/c++-faq-lite/const-correctness.html#faq-18.14 : Even if the language outlawed const_cast , the only way to
I wish to capture data from http://www.kitco.com/texten/texten.html : <html> <head> <title>Text Only Market Page</title>
From http://www.jibbering.com/faq/faq_notes/closures.html : Note: ECMAScript defines an internal [[prototype]] property of the internal Object
http://www.parashift.com/c%2B%2B-faq/cpp-objs-passed-to-c.html The answer is in two parts. First part I guess is till the
After reading monkeytalk faq from http://www.gorillalogic.com/testing-tools/monkeytalk/documentation/monkeytalk-faq : How does it all work? MonkeyTalk is
this is copied from http://www.zenspider.com/ZSS/Products/RubyInline/Readme.html , the home of rubyinline, adding/moding as indicated in

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.