TITLE: what is information hiding (Newsgroups: comp.lang.c++.moderated, 23 Sep 99) BELINGUERES: Gabriel Belingueres > Last night, when reading a C++ book, describing the C++ syntax of a > class. > > The book says that C++ doesn't provides information hiding of the > part of the class definition, and that is supposed to be bad. > > But now, in the age of free source code applications, what is > information hiding good for? > > If a company wants to release the source code of an application, then it > means the source code is for other developers improve on it. So why the > company would want to hide the private part of the class definition? > That makes nonsense. > If a company don't want to release the source code, then the clear view > of the internal variables are a good practice. [*] > > I now, I now. When I was a student, the professors told me that is good, > because it is true that I don't need to know what the instance variables > are, and what they represent to the class. But...it is not true, because > when I use a library or framework, I want to know if they use arrays, or > linked lists, or trees or whatever. > > Personally, I think the C++ conception about information hiding is the > real one. STROUSTRUP: Bjarne Stroustrup I think the real issue is a different one: You place data members in the private part of a class if you want them to be available to the compiler for optimization, inlining, and for allocating variables on the stack. For example: class complex { private: double re, im; public: complex(double r, double i) { re=r; im=i; } complex& operator+=(complex z) { re+=z.re; im+=z.im; return *this; } // ... }; complex operator+(complex a, complex b) { return a+=b; } void f() { complex a(1,2); complex b = a+complex(3,4); } This involves no free store allocations and no function calls. The data hiding offered by "private:" is strictly there to help the programmer enforce proper access to the representation. If you want more hiding - say because you don't want to recompile user code when the representation changes, because you don't want your competitors to see your representation, or because there are many different representations that need to coexist, you simply don't put the representation in the class that users see. Instead, you use an abstract class as the interface. For example: class Device { public: virtual int open(int) = 0; virtual char* write(char*, int) = 0; virtual int read(char*, int) = 0; }; Somewhere else, I can provide an implementation: class Mydevice { // data public: int open(int); char* write(char*, int); int read(char*, int); }; int Mydevice::open(int x) { ... } char* Mydevice::write(char* p, int n) { ... } int Mydevice::read(char* p, int n) { ... } Somewhere else again, you can can provide another: class Yourdevice { // data public: int open(int); char* write(char*, int); int read(char*, int); }; int Yourdevice::open(int x) { ... } char* Yourdevice::write(char* p, int n) { ... } int Yourdevice::read(char* p, int n) { ... } Naturally, this is described in detail in "The C++ Programming Language" and in "The Design and Implementation of C++" and in some of my papers. In particular, "Why C++ is not just an Object-Oriented Programming Language" goes into some detail about the different ways of using classes. [ See http://www.research.att.com/~bs/papers.html -adc ] [ This is a private correspondence response to the section marked [*] above from BELINGUERES. ] TRIBBLE: David R Tribble , 1 Oct 99 This misses the point of data hiding entirely. How would you explain the fact that a programming team writes many classes, each with hidden data, but yet all of the team members have access to all of the source code? Or in the extreme case, a single programmer writing several classes, all of which contain hidden data? The point of "data hiding" is not to hide the source behind the class data, but rather to disallow clients of a class to alter the contents of a class object without going through the "proper channels". In other words, a class with private members is merely protecting them from the outside world (i.e., all of the users of the class), giving the class total control and ownership over the members. The class provides accessors or other methods for retrieving and modifying the members, but all under strict control of the class interface. In other words, data hiding is a concept used to protect parts of programs from affecting other parts that they really shouldn't be concerned with. Open source is orthogonal to this concept, being a concept of people's access to source code, as opposed to code's access to other code or data.