Search code examples
c++information-hiding

Information Hiding C++


What is the principle of information hiding and how does C++ support it. Also does C++ allow me to violate information hiding?


Solution

  • It supports information hiding by allowing private: and protected: sections in class declarations.

    A "supported" way to violate it is via the friend keyword, that allows external functions or classes to access the private and protected members of a class (although it's debatable if that's actually a "violation").

    Also, in a C++ program there's no runtime enforcing of the visibility rules, so if you manage to get a pointer to an internal field or a function pointer to an internal method nothing stops you from using it (again, this may be intentional - the class itself gave you that pointer - or "abusive" - you have a pointer to the object itself and add some offset to get to an internal member).