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.