I write the following code. It seems that a nested class (inner class) can access its parent class's private member. BUT in the book <thinking in C++> 's chapter :introduction to template, it mentions that we must declare the nested class as friend. And says this is the style how STL's iterator is implemented.
Is this true?
Code:#include <iostream> class MyVector{ private: int x; public: MyVector():x(0){} //class iterator; //friend class iterator; // do we really need to make it a friend? class iterator{ public: iterator(){ } void foo(MyVector& a){ a.x=4; return; } }; void print(){ std::cout<<x<<std::endl; } }; int main() { MyVector obj; MyVector::iterator nested; nested.foo(obj); obj.print(); return 0; }



LinkBack URL
About LinkBacks



CornedBee