Const usage in C + + (2)

Source: Internet
Author: User


I've written a blog before. Introduction to const Usage: const usage in C + +


Today I found a busy point, I hereby add.


As we know, there are three cases in which the general const modifies pointers.

<span style= "FONT-SIZE:18PX;" >const int *p;</span>
This means that P points to a const variable of type int, but the pointer itself is not a const.


int a = 0;int *const p = &a;

This condition indicates that the pointer is const and cannot point to additional data once initialized.


int a = 0;const int *const p = &a;

This situation means that pointers and pointers to objects are const.



But here's the problem.

If you use a TypeDef, such as this

typedef int *INTP;CONST INTP p;

So what type of pointer is P?


My answer without hesitation is a normal pointer, but an int variable that points to Const.


In reality, however, p is a const pointer, and the variable it points to is not a const variable. Because INTP is a data type, an int pointer type, const modifies the pointer type, and therefore is a const pointer, the two lines above are equivalent to

int *const p;


Const usage in C + + (2)

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.