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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T06:06:03+00:00 2026-05-31T06:06:03+00:00

$ cat inheritance.cpp #include <iostream> using namespace std; class A { }; class B

  • 0
$ cat inheritance.cpp 
#include <iostream>

using namespace std;

class A { };
class B : private A { };

int main() {
    A* ab = new B;
}
$
$ g++ inheritance.cpp
inheritance.cpp: In function 'int main()':
inheritance.cpp:9: error: 'A' is an inaccessible base of 'B'
$

I just do not understand this error.

As I understand, and as this tutorial confirms, private inheritance should only change how the members of class B are visible to the outside world.

I think the private specifier is doing more than just change visibility of class B members here.

  • Why do I get this error when inheriting and what does it mean?
  • Basically what is wrong with allowing this type of code in C++? Looks totally harmless.
  • 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-31T06:06:05+00:00Added an answer on May 31, 2026 at 6:06 am

    By making the inheritance private, you’re basically saying that even the fact that B inherits from A (at all) is private — not accessible/visible to the outside world.

    Without getting into a long-winded discussion of what would happen if it was allowed, the simple fact is that it’s not allowed. If you want to use a pointer to base to refer to an object of derived type, then you’re pretty much stuck with using public inheritance.

    Private inheritance is not necessarily (or even normally) intended to follow the Liskov substitution principle. Public inheritance asserts that a derived object can be substituted for an object of the base class, and proper semantics will still result. Private inheritance does not assert that though. The usual description of the relationship implied by private inheritance is “is implemented in terms of”.

    Public inheritance means a derived class maintains all the capabilities of the base class and potentially adds more besides. Private inheritance often means more or less the opposite: that the derived class uses a general base class to implement something with a more restricted interface.

    Just for example, let’s assume for the moment that the containers in the C++ standard library were implemented using inheritance rather than templates. In the current system, std::deque and std::vector are containers, and std::stack is a container adapter that provides a more restricted interface. Since it is based on templates, you can use std::stack as an adapter for either std::deque or std::vector.

    If we wanted to provide essentially the same with inheritance, we would probably use private inheritance, so std::stack would be something like:

    class stack : private vector {
        // ...
    };
    

    In this case, we definitely do not want the user to be able to manipulate our stack as if it were a vector. Doing so could (and likely would) violate the expectations of a stack (e.g., the user could insert/remove items in the middle, rather than a purely stack-like fashion as intended). We’re basically using vector as a convenient way to implement our stack, but if (for example) we changed the implementation for stack stand alone (with no dependence on a base class) or re-implement it in terms of std::deque, we do not want that to affect any client code — to the client code, this is supposed to be just a stack, not some specialized variety of vector (or deque).

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

Sidebar

Related Questions

Inheritance.java public class InheritanceExample { static public void main(String[] args){ Cat c = new
cat main.c #include <stdio.h> #include <stdlib.h> #include <linux/ext2_fs.h> int main(int argc, char** argv) {
// main.cpp class Cat() { public: Cat() { a = 0; (*b) = 0;
$ cat t.cpp int sign(int i) { if(i > 0) return 1; if(i ==
$ cat Const.java public class Const { String Const(String hello) { return hello; }
I want to cat all the files in a directory, but include some spacer
I am using cat *.txt to merge multiple txt files into one, but I
In my class, I want to include multiple modules. Each module can define its
I have an Animal class that implements IDomainObject . There is another class, Cat
I am using Oracle. I have two tables: Cat (cat_ID, cat_name, cat_age, cat_strength) Dog

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.