About packaging - an interesting C ++ example

xiaoxiao2021-03-06  60

An interesting phenomenon, from 9CBS Blow Cloud Blog "C from scratch (12) - What is object-oriented programming ideas" original http://blog.9cbs.net/chuiyun/archive/2004/11/26/194722. ASPX.

The reason why this article is because I strange the examples of the following examples that can really compile - the private of the category in the referenced file can directly access the private variables directly, and can access and assign values. Here is the original text of the extracted, interested friends may also try the code test.

-------------------------------------------------- -----------------

Encapsulate

First, see the following types of designs that are often seen in the various VC tutorials about objects. class Person {private: char m_Name [20]; unsigned long m_Age; bool m_Sex; public: const char * GetName () const; void SetName (const char *); unsigned long GetAge () const; void SetAge (unsigned long); Bool getsex () const; void setsex;}; top all the members variables are all defined as private, then provide three pairs of Get / Set functions to access three member variables above (because they are private, the outside world cannot be directly Access), these three pairs of functions are public, why is this? Those textbooks referred to here as package, is packaged in the package of Person's internal memory layout, so that the outside world does not know how it is layout in memory and in turn to ensure the effectiveness of memory (acting only by the class itself). The first thing to confirm the absurdity of the above design, it is authentic "there is no lock" is meaningless. Then look at the so-called package of memory layout. Recalling why each of the start files of the source file to use the class will contain the appropriate header files in "C from scratch (ten)". Suppose is the declaration in Person.h, then use the class Person in B.CPP, I have to be included in #include "person.h", now replace it: Class Person {public: char m_name [20]; unsigned long M_age; bool m_sex; public: const char * getname () const; void setname; unsigned long getage () const; void setage (unsigned long); Bool getsex () const; void setsex (bool); The class Person is then used in B.CPP, as follows: Person A, B; A.M_age = 20; B.GetSex (); here, use Person :: m_age, even if you don't do this, you don't do this. Still #include "person.h", as follows: struct person {char m_name [20]; unsigned long m_age; bool m_sex;}; person a, b; person * pp = (person *) & a; pp-> m_age = 40 The above still modifies the members of Person's instance A, how can I hide the memory layout? ! Recall the role of the statement, the memory layout of the class is necessary when the compiler generates an object, and you can't hide anything about the object's implementation at all, otherwise the compiler cannot compile the corresponding code. Then look from semantics.

Person map is not the concept of people in the real world, should be a buffer in the table in the table of a recorder information in a database, then the buffer should have the function represented by the three pairs of GET / SET? ? The buffer is used by buffer data. After the buffer is used by other operations, it is like a box, just putting the way. Therefore, the top three pairs of GET / SET do not exist, and three member variables cannot be private. Of course, if the Person map is not a buffer, but in other worlds, there is no problem like the semantics like above, it is no problem like it is, but if it is because of the encapsulation of the memory layout, it is big. Error. The above error is worth not understanding. To illustrate the package, look at the MFC (Microsoft Foundation Class Library - Microsoft Function ", a library file defining many classes, which is packaged. About library files in Illustrating SDK) Definition of class CFILE. From the name, it can be seen that it maps the concept of files in the operating system, but it has such a member function - CFile :: Open, CFile :: Close, cfile :: read, cfile :: write, what is the problem ? The four member functions maps are functions for the operation of the file instead of the file, which opens the file, closes the file, writes data from the file, and write data to the file. Is this not a semantic opposite of the member function? The above four operations have a commonality, which is applied to the resource of the file, which can be called "function", such as the file with "opened", with "read" function, but should pay attention to They are not actually the function of the file. According to the original statement, the file should be mapped into a structure, such as File, then the above four operations should be mapped into four functions, then use the functionality of the namespace, as follows: Namespace ofile {BOOL Open (file &, ...); bool Close (File &, ...); Bool Read (File &, ...); Bool Write (File &, ...);} The above name Space Ofile indicates that the four functions in the file are the operation of the file, but the four functions have one File & parameters. Recalling that the non-static member function has a hidden parameter this, so a great idea is born. Describe all the sets of operations for some resource as a resource, map it into a class, the object of this class is the operation of an object, this method is called a package, and that class is called Packaging class or package class. Obviously, the packaging class maps "Operation to a resource" is an abstract concept, that is, the object of the packaging class is a stateless object (referring to the logically stateless object, but if multiple operations are connected It may still be status, but at this time it varies accordingly. Such as one CFile :: FLUSH member function, used to refresh the buffer content, then there is at least one state-buffer, It can also have a status record that has been called CFILE :: Write, no refresh is not needed). It is now possible to understand the meaning of the package. The operation of some resource is encapsulated into a class. This packaging class map is not a "named" concept "defined in the world, but the world's" words "or algorithm" is a concept of a concept " This person is abstract concept.

转载请注明原文地址:https://www.9cbs.com/read-82741.html

New Post(0)