Why when a nested class and its inheritance from the class in which it is nested private variables are inherited? And also why if you remove inheritance ( : public baseclass ), then private variables continue to be displayed in intellisense, only I cannot use them? My main question is how to avoid this inheritance of private variables while I need nested classes?

 #include <iostream> class baseclass { public: class derivedclass; private: int inn = 100; }; class baseclass::derivedclass : public baseclass { public: derivedclass() { std::cout << inn << std::endl; } }; int main() { baseclass::derivedclass first; system("pause"); return 0; } 
  • one
    And what is the general sense to define a derived class as a nested class of a base class? How can a base class know how many derived classes it will have and how they will be defined? I do not see the point in this approach. - Vlad from Moscow
  • one
    And how can they (private variables) not be inherited? The generated class should be able to work instead of the base class, how to do it without inheriting all the fields? - VladD

1 answer 1

derivedclass does not inherit private variables. Because He simply sees the derivedclass nested inn and can use it internally. Make a new class, but not nested in the base class, and inherit it and you will see that only the derivedclass class is inherited. it has a public access modifier.