nested-classes/nested-3.cpp - Inner attempts to access to non-static variable in Outer - does not compile. | Lecture 5 - slide 12 : 40 Program 4 |
// For exercise. // Inspired from page 851 of "The C++ Programming Language", 3ed version. // Can Inner see an instance variable in Outer? By just looking into the outer scope. // The answer is no. The variable must be static for this to be succesful. #include <iostream> #include <string> class Outer{ private: int private_i; public: int public_i; class Inner{ public: void fi(Outer *p); }; }; void Outer::Inner::fi(Outer *p){ int local1 = private_i * 2; // error: invalid use of nonstatic data member Outer::private_i int local2 = public_i * 3; // error: invalid use of nonstatic data member Outer::public_i std::cout << local1 << ", " << local2 << std::endl; } int main(){ Outer o; Outer::Inner i; i.fi(&o); }