How to make available the private field of a nested class only in the external class?
template <typename T> class A { public: class B { private: T* bar; public: //... }; void test(T data) { B foo(data); T* p = foo.bar; //нет доступа } };
How to make available the private field of a nested class only in the external class?
template <typename T> class A { public: class B { private: T* bar; public: //... }; void test(T data) { B foo(data); T* p = foo.bar; //нет доступа } };
You need to declare class A
to be a friend of class B
:
template <typename T> class A { public: class B { friend class A; // A друг B private: T * bar; public: //... }; void test( T data ) { B foo( data ); T * p = foo.bar; // доступ есть } };
In any case, you have a design problem, and the recommendations to plant a getter, or, even worse, arrange friendship classes, are very dangerous - read Golub (tip 117), it describes why - by making a friendly class, you will give your friend the full right to dig into all fields of parent-friend . Subversion of encapsulation is obvious.
In short, the entire object-oriented design is needed to manage complexity by, in particular, encapsulation. And you are advised to break it.
UPD: The answer to the question, in this case, is the phrase "you have a problem in design"
Source: https://ru.stackoverflow.com/questions/451167/
All Articles
B
from public to private, and make the field public? - VladDB
should be public - defias