【发布时间】:2019-12-12 23:47:06
【问题描述】:
我在这里有一个简单的例子:我使用类型别名使用using 关键字作为引用类型然后我想知道我是否可以使用该类型别名和指针运算符(*)来声明对指针的引用:
int main(){
using ref_int = int&;
int x = 10;
int* p = &x;
//int*(&rpx) = p;
//ref_int * rptrx = p; // pointer to reference is not allowed.
*ref_int(rptrx) = p; // rptrx is undefined
}
-
因为好奇,当我使用
std::vector<int>::reference的 Element-type 时,我想将它与指针运算符*结合起来声明对指针的引用:int* ptr = new int(1000); std::vector<int>::*(reference rptr) = ptr; // error: expected expression -
但我可以使用指针类型别名结合引用运算符“&”来声明它:
using pInt = int*; int i = 57; int* ptrI = &i; pInt(&rpInt) = ptrI; cout << *rpInt << endl;
** 我知道我不能有指向引用的指针,因为引用只是已经存在的对象的别名,而指针是对象,因此我们可以拥有指针或对它的引用。
【问题讨论】:
标签: c++ pointers reference type-alias