Reference to the const pointer

Source: Internet
Author: User

http://bbs.csdn.net/topics/310047017


Int * is a pointer to an int type. The
Const int* is a pointer to the const int type. The
int *const A constant pointer to the type int. The
Const intx& is also a different type. (reference to a constant pointer) The
int* const& a constant reference to the pointer.
Const int* const& A constant reference to a constant pointer


When you call the
Void fun (const int *&)-> The type of the request parameter: a reference to a pointer to the const int type




int *b = new int (0);  
Fun (b);//error


The incoming parameter is int* if you want to Intx Turning to a const int*&, you need to convert int* to const int* and then convert the const int* to const int*, which requires a two-step conversion.
In fact, the compiler's default conversion can only support one step of the conversion!! More than two-step conversion, is not supported.
When the compiler is unable to find a way to successfully convert a method, it will naturally be an error.


When you write out the code,
int *d = new int (0);  
Fun (const int *) d;//error 
Delete d; 


When you use fun (const int *) d, there is no doubt that you introduce a temporary variable, and when you want to refer to a temporary variable, the reference must be const, and the temporary variable must be referenced by a const.
and the declaration of your function requires a reference to a constant pointer, not a const reference, so the reference fails.


Of course, if you call this:
int *e = new int (0);  
Fun ((const int *&) e);//ok 
Delete e; 
You create a temporary reference, but the variable type is the same as the argument type of the fun declaration, so it's OK.

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.