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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T05:47:42+00:00 2026-06-18T05:47:42+00:00

Consider: class A { protected: int _i; }; class B : public A {

  • 0

Consider:

class A {
protected:
    int _i;
};

class B : public A {
    B(const B & object) {
        _i = object._i;
    };

    B(const A & object) {
        _i = object._i;
    };
};

First copy constructor is correct, because permissions are based on classes, not on objects. So I can reach protected members from my own class objects.
Why second constructor (or any similar method) raises compile time error?

In short: Why permission checking in C++ does not regard class inheritance rules in this case?

Also this was encountered in Apple LLVM-4.2 but not in Apple LLVM-4.1.

  • 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-18T05:47:44+00:00Added an answer on June 18, 2026 at 5:47 am

    In short: Why permission checking in C++ does not regard class inheritance rules in this case?

    You’re asking for rationale behind the rule which prohibits the second constructor.

    To understand the rationale, consider this code:

    A a;
    a._i = 100; //error, _i is protected 
    

    That is correct, and expected. So far, so good.

    But if the second constructor is allowed (in your code), then anyone can write a class modify as:

    struct modify : public A {
        void i(A & object, int newValue) {
            object._i = newValue; //MODIFY THE PROTECTED MEMBER of the argument!
        };
    };
    

    And then you can do this instead:

    A a;
    modify().i(a, 100); //okay, indirectly modify a._i !!!
    std::cout << a._i << std::endl; //prints 100
    

    If the second constructor can access the protected member of the argument, then modify::i() can also do the same! That way you actually modify the protected members!

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

Sidebar

Related Questions

Consider this syntactically correct(?) pseudocode: class Event { public: virtual int getID() const =
Consider this: public class interface Person : IPerson { int ID { get; protected
Consider the following code: #include <iostream> class Test { public: constexpr Test(const int x)
Consider the following: class MyClass { public: int operator ()(int a, int b); };
Consider the following class public class Class1 { public int A { get; set;
Consider this piece of code: public abstract class Validator { protected Validator() { }
Consider you have the following code: public abstract class MenuItem { protected string m_Title;
Consider the following code: using System.IO; using System.Threading.Tasks; public class MyClass { private int
Consider the following example : class MyClass { // Getters by copy ? public:
Consider the following example : template<int X> class MyClass { public: MyClass(int x) {_ncx

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.