Pointer and reference in C ++ (1)

Source: Internet
Author: User

Pointer and reference in C ++ (1)

1. pointer and reference definition

Before in-depth introduction, let's take a look at the definitions of pointers and references, the differences between pointers and references, and then discuss the differences between pointers and references in depth.

  • Authoritative pointer definition:

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.

-- Excerpt 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-referenced data members of the const class object are const-qualified;

The non-static and non-referenced data member of the volatile class object is volatile-qualified;

The non-static and non-referenced data member of the const-volatile class object is const-volatile-qualified.

When CV-qualifiers is used to limit the array type, the array member is actually limited by the CV-qualifiers instead of the array type.

The composite type is not limited by the CV-qualifier because its members are limited by the CV-qualifier. That is to say, even if the composite type members are limited by the CV-qualifier, this composite type is not a CV-qualified object.

  • Reference authoritative definition:

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 when T 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.

-- Excerpt from ansi c ++ Standard

The above definition is hard to understand. If so, it means you are not familiar with C ++, and you still have a long way to go. The following is a brief introduction:

  • Pointer-for a type T, T * is the pointer type pointing to T, that is, a T * type variable can save the address of a T object, type T can be added with some restrictions, such as const and volatile. See the following figure for pointer meanings:

  • Reference-a reference is an alias of an object. It is mainly used for function parameters and return value types. The symbol X & indicates a reference of the X type. See, for the meaning of the reference:


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.