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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:38:42+00:00 2026-05-27T02:38:42+00:00

How can I access the data in a parent’s class, which is protected, when

  • 0

How can I access the data in a parent’s class, which is protected, when passed into a derived class.

class parent
{ 
    protected:
        int a;
};

class child : public parent
{
    void addOne(parent * &);
};

void child::addOne(parent * & parentClass)
{
    parentClass->a += 1;
}

int main()
{
    parent a;
    child b;

    parent* ap = &a;

    b.addOne(ap);
}
  • 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-27T02:38:42+00:00Added an answer on May 27, 2026 at 2:38 am

    You cannot access protected data via a pointer/reference to the base class. This is to prevent you from breaking the invariants that other derived classes may have on that data.

    class parent
    {
        void f();
        // let's pretend parent has these invariants:
        // after f(), a shall be 0
        // a shall never be < 0.
    
        protected:
            int a;
    };
    
    class child : public parent
    {
    public:
        void addOne(parent * &);
    };
    
    
    class stronger_child : public parent
    {
    public:
        stronger_child(int new_a) {
            if(new_a > 2) a = 0;
            else a = new_a;
        }
        // this class holds a stronger invariant on a: it's not greater than 2!
        // possible functions that depend on this invariant not depicted :)
    };
    
    void child::addOne(parent * & parentClass)
    {
        // parentClass could be another sibling!
        parentClass->a += 1;
    }
    
    int main()
    {
        stronger_child a(2);
        child b;
    
        parent* ap = &a;
    
        b.addOne(ap); // oops! breaks stronger_child's invariants!
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Access can open DBF (dBase) files, but instead of physically converting the data into
How i can insert data into more than one table of an Access database
I'm trying to pull in data from a remote SQL Server. I can access
How can I use Enterprise Library to do a Data Access Layer in c#
I'm looking for a way to create a data access layer that can either
How can I generate a report in access with the data from a recordset
can you use ms access as the backend of a asp.net dynamic data project
Can anyone give me some idea about uploading data from excel to access database
I can make a DAO recordset in VB6/Access do anything - add data, clean
I'm trying to access some custom data emitted into an SVG document by Visio

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.