Const Small summary of C + + (3)

Source: Internet
Author: User

Const Small summary of C + + (2)

Today, we discuss the relationship between a pointer or a referenced shape participating in a const.

First of all: in C + +, the parameters of the same name function must be clearly distinguished.

For example, a function with the same name as void reset (int &i) and void reset (const int &i) is not compiled.

 int  i = 0   const  int  ci = I;  string :: Size_type ctr = 0  ;reset ( &i); Reset ( &ci); //  error, cannot be initialized with a pointer to a const int object int*  reset (i); reset (CI);  //  error, cannot bind ordinary reference to const object CI on  reset (42 ); //  error, cannot bind ordinary reference to literal value  Reset (CTR); //  error, type mismatch, str is unsigned type  

We can see that there are many limitations when using a function without constant reference.

There are two drawbacks to using normal references in functions that do not change the parameters:

    1. The user of a function is misleading: a function can modify the value of its argument.
    2. Greatly restricts the types of arguments that a function can accept. We cannot pass const objects, literals, or objects that require type conversions to ordinary reference arguments.

We'll get this sorted out today.

Const Small summary of C + + (3)

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.