Tag: inheritance
-
Why does the derived class have access to the private field of the base class?
10 class Base { public: class FirstBase { friend class Base; int x = 10; }; class SecondBase : public FirstBase { public: SecondBase() : FirstBase() {} void t() { std::cout << FirstBase::x; } }; }; This code compiles and works, but I don’t understand why it works. Can explain or cite sources to read?…
-
Passing a pointer to a type that is privately inherited in some base class
18 I always assumed that private inheritance simply means that a type doesn’t tell the outside that it’s inheriting from some base class. However, it seems that there are more restrictions. Consider the following minimal example: struct MyInterface {}; struct MyImpl : private MyInterface {}; struct Inherited : public MyImpl { // Error: ‘MyInterface’ not…
-
In C++ Inheritance, Derived class destructor not called when pointer object to base class is pointed to array of derived class
17 I have an Animal class with constructor and destructor. Cat have a private Brain* attribute. Upon construction, Cat creates his Brain using new Brain(); Upon destruction, Cat deletes his Brain. I don’t understand why The cat’s and brain’s destructors not called, when my Base class destructor is virtual? #include <iostream> using std::cout ; using…