How does C++ use T* or onwer to specify a unique object?
This article mainly introduces "how C++ uses T* or onwer to specify a unique object". In daily operation, I believe many people have doubts about how C++ uses T* or onwer to specify a unique object. Xiaobian consulted all kinds of materials and sorted out simple and easy-to-use methods of operation. I hope it will be helpful to answer the doubts about "how C++ uses T* or onwer to specify a unique object". Next, please follow the editor to study!
F.22: Use T* or owner to designate a single object (using T* or owner to indicate a unique object) Reason (reason)
Readability: it makes the meaning of a plain pointer clear. Enables significant tool support.
Readability: this makes the meaning of the bare pointer clearer. Make important tool support effective.
Translator's note: owner is a function provided by gsl (guidelines support Library), which is consistent with the meaning of T* from a compilation point of view, but with ownership semantics that can help programmers understand code and tool checks.
Note (Note)
In traditional C and C++ code, plain T* is used for many weakly-related purposes, such as:
In traditional C and C++ code, bare pointers are used for many unrelated purposes, such as:
Identify a (single) object (not to be deleted by this function)
Represents a (single) object (will not be deleted by this function)
Point to an object allocated on the free store (and delete it later)
Point to an object obtained from free storage (which will be deleted later)
Hold the nullptr
Hold nullptr
Identify a C-style string (zero-terminated array of characters)
Represents a C-style string (an array of characters ending in 0)
Identify an array with a length specified separately
Represents an array with a length defined separately
Identify a location in an array
Represents the first address of the array
This makes it hard to understand what the code does and is supposed to do. It complicates checking and tool support.
The result is that it is difficult to understand what the code is doing and what it is expected to do. Both inspection and tool support can be complex.
Example (sample)
Void use (int* p, int n, char* s, int* Q) {p [n-1] = 666; / / Bad: we don't know if p points to n elements; / / assume it does not or use span cout