C ++ programming ideology notes

zhaozj2021-02-16  43

Chapter 3 Hide Realization First, C Access Control

Public means that all members of the subsequent statements can access all people. Public members are like a general Struct member. The Private keyword means that anyone cannot access these members in addition to the creator and the internal member functions of this type. Private built a wall between the designer and the user. If someone tries to access a private member, it will generate a compilation error. Protected is basically similar to private, only a little difference: inherited structure can access Protected members, but cannot access Private members.

Second, a reference to a certain class in advance: Solution is to note Y :: f (x *) references the address of an X object. This is critical because the compiler knows how to pass an address, which is certain, regardless of the type of object being passed. If you try to pass the entire object, the compiler must know all the definitions of X to determine its size and how to deliver it, this makes the programmer unable to declare a function similar to Y:: g (x).

Third, the handle class (see Effective C , 9CBS problem http://www.9cbs.net/expert/topic/576/576745.xml?temp=.1663324)

In our programming environment, when a file is modified, or the header containing the file contained in the file is modified, the project person in charge needs to be repeatedly compiled. This means that whenever the programmer has modified a class, whether it is modified a public interface part, or a private implementation section, he has to compile all the files that contain the header file. For a large project, this may be very difficult to handle in the initial development of the development, because the implementation may need to be changed frequently; if this project is very large, there is too much time to compile, it may hinder the completion of the project. Solving this problem with techniques sometimes called the handle classes or "cheshire cat" [1]. Anything about the implementation disappeared, leaving only a single pointer "smile". The pointer points to a structure that appears in the implementation file as the definition of all its member functions. In this way, the header file does not need to change as long as the interface portion does not change. The implementation section can be forth in need, and then re-compile the implementation after completion, and then connect to the project. Here is a simple example of this technology. Only public interfaces and a simple specified class pointer are included in the header file.

Chapter 4 Initialization and Clear

First, spatial distribution

The compiler is more likely to allocate all memory at the beginning of a block, just like a C compiler. These are irrelevant to us, because as a programmer, we can't always get storage space before variable definition. Even if the storage space is assigned at the beginning of the block, the constructor will still be called when the object is defined, as the identifier is only valid. Here me: The distribution of memory space is assigned at the beginning of the block, but the programmer's use must wait until the identifier is declared, and only when his constructor is called!

Chapter 5 Function Overload and Default Parameters

First, the default function: Two rules must be remembered when using the default parameter. First, only the rear parameters of the parameter list can be default, that is, we cannot follow a non-default parameter behind a default parameter. Second, once we start using the default parameters, all parameters behind this parameter must be default.

Second, placeholder

Most compilers give a warning message and think that we have made a logical error. This warning can be prevented with this parameter without name. More importantly, if we start using a function parameter, then discovering that it doesn't need to use it, we can efficiently remove it without calendering errors, and do not need to change the program code that calls the function previous version. Chapter 6 Input Output Flow

First, manipulation calculative

A manipulation calculator called E N D L. A manipulation calculator acts on the flow, in which case it is inserted into a new row and empty the stream (eliminating all the words stored in the internal stream buffer). You can also empty the stream: ccout << flush; otherwise a basic manipulation operator turns the base to OCT (octal), DEC (decimal) or HEX (hexadecimal): cout << Hex << "0x" << i << endl; has a manipulation calculator for extract "skip" space: CIN >> WS;

Second, the input and output flow

An ISTREAM or an OSTREAM. There are different types of input streams and output streams: file input stream (ifstreams) and file output stream (OFSTREAMS), CHAR * memory (kernel formatted) input stream istrstreams and output stream (Ostrstreams), and with standard C strings StringStreams and StringStreams of class interfaces.

Third, open-out input GET, GETLINE, Read, Write

There are two options: member functions get () or getLine (). Both functions have three parameters: pointing to the buffer pointer of the storage result character, the buffer size (cannot exceed its limit) and the terminator who knows when the read input is stopped. The terminator has a default value "/ n" that is often used. When the two functions encounter the input terminator, the zero storage is stored behind the buffer. Although the difference is small but extremely important: GET () stops when the separator of the input stream is stopped without extracting the separator from the input stream. If you use the same separator to call a GET () function, it will return immediately without any input. (Either in the next GET () description with a different separator, either use a different input function). GetLine () is contrary to it, it extracts the separator from the input stream, but still does not store it in the result buffer.

Read the original byte: If you want to know what is being processed, you can use the READ () function to move bytes directly into the memory in the memory. The first parameter is a pointer to the address destination address, and the second parameter indicates the number of bytes to read. Store in advance in one file is particularly useful.

Fourth, the input output stream buffer (STREAMBUF pointer in the RDBUF - area)

Each input and output stream contains a pointer, pointing to some STREAMBUF (which depends on whether it handles standard I / O, files, memory, etc.). We can access streambuf directly. For example, you can move into the streambuf, remove the original byte, without having to format them by inputting the output stream. In order to allow us to access streambuf, each stream object has a member function called Rebuf (), which returns a pointer to the StreamBuf pointing to the object. In this way, we can call any member functions for the next StreamBuf. One of the most interesting things made by the StreamBuf pointer is to use the "<<" operator to stream with another input output. This enables all bytes in our object into the objects of "<<". This means that if all bytes of an input / output stream are moved to another input output stream, we don't have to do one byte of their bytes or a monotonous job. This is a first-class method. 5. Find in the input and output stream (TELLG, TELLP, Seekg, seekp)

In some cases, it may be necessary to move this stream, can be processed in two ways: The first way is absolutely positioning, streaming stream positioning (streambuf) in the stream; second mode like a standard C library function FSEEK Then, the reference number is moved from the beginning of the file, the end, or the current position.

Six, establish a read / write file

The following code first creates a marker's ifstream, which is both an input file and an output file. The compiler will not let us write to ifstream, there is a need to establish an output stream with basic stream buffers (ostream). Here you should Note that ifstream and Ostream must establish a association! Then change to ostream will also change to IFStream. Ifstream in ("in.txt", ios :: in | ios :: out); ostream out (in.rdbuf ());

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

New Post(0)