Confusing const-C ++ primer Reading Notes

Source: Internet
Author: User
An attempt to point a non-const object pointer to a constant object will cause a compilation error.

Const double * cptr
Cptr is a pointer to a double-type const object. (We can read this definition from right to left as "cptr is a pointer to a double type object defined as const ".) The subtle difference is that cptr itself is not a constant. We can re-assign cptr to point to different objects, but cannot modify the objects pointed to by cptr.

In the actualProgramThe pointer to const is often used as a function form parameter. It serves as a Convention to ensure that the actual objects passed to the function will not be modified in the function. For example:
Int strcmp (const char * str1, const char * str2 );

Int * const curerr = & errnumb
Curerr is a const pointer to a non-const object. (We can read the definition from right to left as "curerr is a const pointer to an int type object ".) This means that it cannot be assigned to curerr for other local values, but you can modify the value pointed to by curerr.

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.