-4
class a
{
public:
    int var;
};

class b : a
{
};

int main()
{
    a* p = new b();

    return 0;
}

I know its caused by private inheritance but i wanna know why is it so?

  • 1
    Possibly a duplicate of https://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance – StoryTeller - Unslander Monica Sep 28 '19 at 13:24
  • 2
    Possible duplicate of [Difference between private, public, and protected inheritance](https://stackoverflow.com/questions/860339/difference-between-private-public-and-protected-inheritance) – FL3SH Sep 28 '19 at 13:35
  • 1
    It does it because that's what the standard says. – Omnifarious Sep 28 '19 at 13:35
  • i think my question is being misunderstood, I'm not trying to access any private member outside the class, or any protected one outside class hierarchy, I'm just trying to create a dynamic object with a base class pointer and it raises an error – Alex Mercer Sep 28 '19 at 14:00
  • @AlexMercer - It's not being misunderstood. And while I can't speak for others, I can say that what I find confusing about your question is what your question is. The behavior is the expected behavior. The language is operating exactly as it was designed to. If you have a question about how to accomplish a particular design goal within the framework of the language given that this attempt you made doesn't work, ask that question. As it is, nobody has any idea what question you're asking. – Omnifarious Sep 28 '19 at 15:45

2 Answers2

0

You explicitly specified that the base class is inherited as a private sub-object of the derived class. So it is inaccessible outside the class definition.

From the C++ 17 Standard(14.2 Accessibility of base classes and base class members)

  1. ...If a class is declared to be a base class for another class using the private access specifier, the public and protected members of the base class are accessible as private members of the derived class.

In this declaration

a* p = new b();

there is an attempt to access the private sub-object of the type a of an object of the type b because the static type of the pointer p is a *.

In fact this statement

a* p = new b();

has the same semantic as for example

class A
{
private:
    int x = 10;
};

A a;
int *p = &a.x;

If this was allowed then using a pointer you could change a private subobject of an object.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
  • When i create a static object (by declaration) then this error doesn't show up but when i try to make a dynamic object then an error is raised saying that base class a is inaccessible – Alex Mercer Sep 28 '19 at 13:51
  • @AlexMercer The problem is that using the pointer of the type a * you are trying to access the private sub-object of the object of the type b. – Vlad from Moscow Sep 28 '19 at 13:53
  • @VladfromMoscow so where are the `private sub-object` and the `object of type b` ? How to know that `you are trying to access it`? – Hello Everyone Sep 28 '19 at 13:59
  • @HelloEveryone Because the pointer is initialized by the address of the private subobject. – Vlad from Moscow Sep 28 '19 at 14:01
0

This is just what "private" means in private inheritance. The fact that b inherits from a is an implementation detail and not visible from outside.

I'm not trying to access any private member outside the class, or any protected one outside class hierarchy, I'm just trying to create a dynamic object with a base class pointer and it raises an error

You are trying to access a conversion from a b* to a a* but you cannot, because from outside, the base class is not accessible. Private inheritance is closer to composition rather than public inheritance. Your example could be rewritten as

class b {
      a a_instance;
};

The implementation of b will be slightly different (because call to a member functions is via a_instance instead of this), but the effect is the same: We compose b of a and users have no knowledge of that (aka private).

463035818_is_not_an_ai
  • 109,796
  • 11
  • 89
  • 185