In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-26 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/03 Report--
This article mainly explains "the definition of pointers and citations in C++". Interested friends may wish to have a look at it. The method introduced in this paper is simple, fast and practical. Now let the editor take you to learn the definition of pointers and references in C++.
1. Definitions of pointers and references
Before going into detail, let's take a look at the definition of pointers and references, the differences between pointers and references, and then discuss pointers and references respectively, and delve into the details of why there are these differences.
Authoritative definition of pointers:
In a declaration T D where D has the form * cv-qualifier-seqopt D1 And the type of the identifier in the declaration T D1 is "derived-declarator-type-list T", then the type of the identifier of D is "derived-declarator-type-list cv-qualifier-seq pointer to T". The cv-qualifiers apply to the pointer and not to the object pointer to.
-- extracted from "ANSI C++ Standard"
Note: some readers may not understand cv-qualifier-seq
CV-qualifiers (CV qualifier)
There are three types of CV-qualifiers: const-qualifier (const qualifier), Volatile-qualifier (volatile qualifier), and const-volatile-qualifier (const-volatile qualifier).
The non-static, non-mutable, and non-reference data members of the const class object are const-qualified
The non-static, non-reference data member of the volatile class object is volatile-qualified
The non-static, non-reference data member of the const-volatile class object is const-volatile-qualified.
When CV-qualifiers is used to qualify an array type, it is actually the array member that is qualified by the CV-qualifiers, not the array type.
A composite type is not qualified by the CV-qualifier because its member is qualified by the CV-qualifier, that is, even if the member of the compound type is qualified by CV-qualifier, the composite type is not a CV-qualified object.
Authoritative definition of reference:
In a declaration T D where D has the form& D1 And the type of the identifier in the declaration T D1 is "derived-declarator-type-list T", then the type of the identifier of D is "derived-declarator-type-list cv-qualifier-seq reference to T". Cv-qualified references are ill-formed except when the cv-qualifiers are introduced through the use of a typedef or a template type argument, in which case the cv-qualifiers are ignored.
-- extracted from "ANSI C++ Standard"
The above definitions are a little difficult to understand at first glance, but if so, it means that you are not familiar with C++ and you still have a long way to go. Here is an overview in easy-to-understand words:
Pointer-for a type T _ volatile * is the pointer type pointing to T, that is, a variable of type T * can hold the address of a T object, while type T can add some qualifiers, such as T, volatile, and so on. See the following figure, the meaning of the pointer shown:
Reference-reference is an alias for an object and is mainly used for function parameters and return value types, and the symbol X & represents a reference to type X. See the following figure, which shows the meaning of the reference:
2. The difference between pointer and reference
First of all, the reference can not be empty, but the pointer can be * face also said that the reference is an alias of the object, the reference is empty-- the object does not exist, how can there be an alias! Therefore, when defining a reference, it must be initialized. So if you have a variable that points to another object, but it may be empty, you should use a pointer; if the variable always points to an object, i.e., your design does not allow the variable to be empty, then you should use a reference. In the following figure, if you define a reference variable, you will not be able to compile without initialization (compile-time error):
The declaration pointer can not point to any object, and it is for this reason that null operation must be done before using the pointer, but the reference is not necessary.
Second, references cannot change their direction to one object "till death do us part"; but pointers can change their direction and point to other objects. Description: although a reference cannot change the direction, it can change the contents of the initialization object. For example, in the case of the + + operation, the operation on the reference directly reflects the object to which it points, rather than changing the point, while the operation on the pointer causes the pointer to point to the next object, rather than changing the contents of the object it refers to. See the following code:
# include using namespace std; int main (int argc,char** argv) {int iTunes 10; int& ref=i; ref++; cout
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.