问题描述
这是代码:
int main()
{
int* p = nullptr;
const int* cp = p;
const int*& ref = cp;
const int*& ref0 = cp;
const int*& ref1 = p;//not allowed
const int*& ref2 = static_cast<const int*&>(p);//not allowed
int* const & ref3 = p;//allowed. Why?
return 0;
}
运行它时,我得到了
test.cpp:7:14: error: non-const lvalue reference to type 'const int *' cannot
bind to a value of unrelated type 'int *'
const int*& ref1 = p;
^ ~
test.cpp:8:21: error: non-const lvalue reference to type 'const int *' cannot
bind to a value of unrelated type 'int *'
const int*& ref2 = static_cast<const int*&>(p);
我无法理解原因.也许const
在这里起作用.当我写const int*&
或int* const &
时,我不知道const
适用于指针或引用.而且const int* const &
也使我感到困惑.我想知道以下项目的语法:
I am having trouble understanding why. Maybe const
plays a role here. And when I wrote const int*&
or int* const &
, I don't know const
apply to pointer or reference. And const int* const &
confuses me too.I want to know the syntax for the following item:
对const指针的引用.
A reference to a pointer that is const.
对指向const的指针的引用.
A reference to a pointer that points to a const.
对const指针的引用,该指针指向const.
A reference to a pointer that is const and points to a const.
和const参考版本:
And const reference version:
对指针的const引用.
A const reference to a pointer.
对const指针的const引用.
A const reference to a pointer that is const.
指向指向const的指针的const引用.
A const reference to a pointer that points to a const.
const指向const并指向const的指针.
A const reference to a pointer that is const and points to a const.
对不起,我很困惑.
推荐答案
您的p
是指向int
的指针. const int*&
是对const
int
的指针的引用.由于p
是int
而不是const
int
,因此您不能将应该引用指向const
int
的指针的引用绑定到引用p
.
Your p
is a pointer to an int
. const int*&
is a reference to a pointer to a const
int
. Since p
is an int
and not a const
int
, you cannot bind a reference that is supposed to refer to a pointer to a const
int
to refer to p
.
在C ++中读取声明的关键是从内而外"读取它们,即从标识符(名称)开始,然后逐步解决.举个例子:
The key to reading declarations in C++ is to read them "from the inside out", i.e., start from the identifier (name) and work your way out. Take your example:
int* const & ref3;
是对int
的const
指针的引用.我们从标识符(ref3
)开始并向外进行.我们发现的第一件事是&
.因此ref3
是参考.下一个是const
,所以ref3
所指的是const
.下一个是*
,因此引用引用的const
是指针.最后,int
,我们正在处理对int
的const
指针的引用.
is a reference to a const
pointer to an int
. We start at the identifier (ref3
) and work outwards. The first thing we find is an &
. So ref3
is a reference. Next thing is const
, so whatever ref3
refers to is a const
something. Next thing is a *
, so the const
thing the reference refers to is a pointer. Finally int
, we're dealing with a reference to a const
pointer to an int
.
请注意,标识符的两面可能都有东西. 解决问题"时,您必须考虑首先/更强地绑定哪些说明符/运算符,以弄清楚所声明的类型.例如:
Note that you can have stuff happening to both sides of the identifier. When "working your way out", you have to consider which specifiers/operators bind first/more strongly to figure out what type is declared. For example:
int const * a[10];
同样,我们从标识符a
开始. []
的绑定比*
绑定更牢固,因此a
是10个元素的数组. a
是数组的哪些元素?接下来是*
,因此a
是10个指针的数组.最后,我们发现a
是10个指向const
int
的指针的数组.请注意,如果结尾处还有一个寂寞的const
,则该const
会绑定到之前的任何内容.这就是让我们也可以写与int const a
等效的const int a
的原因.
Again, we start at the identifier a
. The []
binds more strongly than *
, so a
is an array of 10 elements. What are these elements that a
is an array of? Next comes a *
, so a
is an array of 10 pointers to something. In the end, we find that a
is an array of 10 pointers to const
int
. Note that if there's a lonely const
left at the end, that const
binds to whatever came before it. That's what allows us to also write const int a
which is equivalent to int const a
.
您还可以使用括号来影响运算符在声明中生效的顺序.例如
You can also use parentheses to influence the order in which operators take effect in a declaration. For example
int const (* a)[10];
将是指向10个const
int
数组的指针,而不是指向const
int
的10个指针的数组.
would be a pointer to an array of 10 const
int
rather than an array of 10 pointers to const
int
.
这篇关于将引用绑定到指针的语法是什么?(所有类型)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!