您好,我正在阅读有关 C++ 中的表达式和整个语句的内容
声明0.0
每个表达式都有一些非引用类型
引用的语句来自 en.cppreference.com/w/cpp/language/value_category。检查页面顶部的第 2 行。
现在我举了一些例子来理解这意味着什么。例如:
int i = 100; // this expression has type int
int &j = i; // this expression has type int or int&?
我的困惑是,我知道 j 是对 int 的引用,即 j 是 int& 但根据引用的语句,每个表达式都有一个非引用类型将意味着int &j = i;
具有 int 类型。它是否正确?
我感到困惑的其他例子:
int a[4] = {2,4,4,9};
a[3]; // will this expression be int& type or int type?
现在在声明中a[3];
我知道 a 是一个数组左值,因此 a[3] 返回对最后一个元素的左值引用。但令人困惑的是引用的语句 0.0 是否意味着整个表达式a[3];
是 int 还是 int& 类型?
这是另一个例子:
b[4]; // Here assume that b is an array rvalue. So will this expression has type int&& or int?
所以我的问题是指针也会发生类似的情况吗?这意味着我们对于指针也有类似的声明(0.0)吗?
int x = 34;
int *l = &x; // will this expression have type int* or int?
我现在这里 l 是一个指向 int(复合类型)的指针。如果对于指针没有类似的声明,那么对于引用还需要这个声明吗?这就是为什么我们只去掉参考部分?