gpt4 book ai didi

c++ - 为什么在这种情况下将此指针视为右值?

转载 作者:行者123 更新时间:2023-12-01 15:06:40 25 4
gpt4 key购买 nike

以下内容不起作用:

std::array<int, 3> arr = {1, 2, 3};
int **ptr = &(arr.data());

因为我要尝试使用右值的地址。我已经解决了:
std::array<int, 3> arr = {1, 2, 3};
int *ptr = arr.data();
int **ptr2 = &ptr;

这似乎工作得很好。

我知道您不能使用右值的地址,但是为什么在这里这样处理呢?是因为将从 arr.data()创建一个临时文件,然后将其分配给 ptr,然后我尝试获取该临时文件的地址吗?

最佳答案

Is it because a temporary will be created from arr.data() and then assigned to ptr and I'd be trying to take the address of that temporary?



是。按值返回的任何值都将返回一个右值。 std::array::data返回 T*,而不是 T*&,因此它的返回值是一个右值。

关于c++ - 为什么在这种情况下将此指针视为右值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60081701/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com