对指向 const 对象的非常量指针的非常量引用 [英] Non-const reference to a non-const pointer pointing to the const object
问题描述
简单来说,我有一个简单的指针:
In simple words I have a simple pointer:
int* a;
现在,我想改变这个指针的值.我想在一个函数中做到这一点.函数确保它不会改变指针指向的对象,但会改变指针本身.这就是为什么我希望这个函数采用如下参数:非常量引用(因为指针的值将被更改)到非常量指针(指针本身可以更改)指向常量对象(函数保证,该对象,指向的那个指针不会改变).
now, I would like to change value of this pointer. I want to do this in a function. Function assures, that it will not change object, that pointer points to, but will change a pointer itself. This is why I would like this function to take argument like: non-const reference (because value of pointer will be changed) to the non-const pointer(pointer itself can be changed) pointing to const object (function assures, that object, that pointer points to will not be changed).
最简单的函数是:
void function(const int*& a){
a = 0;
}
但是当我尝试调用这个函数时:
but when I try to call this function:
int main(){
int* a;
function(a);
return 0;
}
编译器不高兴地说:
类型为const int*&"的非常量引用的初始化无效来自'const int*'类型的右值函数(a);
invalid initialization of non-const reference of type 'const int*&' from an rvalue of type 'const int*' function(a);
我不太明白这个错误,对我来说没有涉及到右值(我传递了一个对对象的引用,它已经存在于堆栈中.)
I cannot quite understand this error, as for me there is no rvalue involved (I am passing a reference to object, that already exists on the stack.)
问题是,我怎样才能正确地做到这一点?
Question is, how can I do it properly?
可以在此处找到示例:https://ideone.com/D45Cid
有人建议,我的问题类似于 为什么将指针到非常量的指针"转换为非法?到指向const指针的指针"
It was suggested, that my question is simillar to the Why isn't it legal to convert "pointer to pointer to non-const" to a "pointer to pointer to const"
我的问题不同,因为我不使用指向指针的指针,我只使用指向对象/值的指针并存储对它的引用,因此情况类似于该问题的答案:
My question is different as I do not use pointer to pointer I use only pointer to object/value and store reference to it, therefore situation like in the answer to that question:
const char c = 'c';
char* pc;
const char** pcc = &pc; // not allowed
*pcc = &c;
*pc = 'C'; // would allow to modify a const object
在我的情况下是不可能的,因为我无法取消引用顶级指针(我没有这样的指针).
Is impossible in my case, as I cannot dereference the top level pointer (I do not have such a pointer).
此外,我质疑了这个问题的漂亮和干净的解决方案,这不在问题中
Moreover I questioned about nice and clean solution to this problem, which is not covered in a question
推荐答案
我不太明白这个错误,因为我没有涉及到右值(我传递了一个对对象的引用,它已经存在于堆栈中.)
I cannot quite understand this error, as for me there is no rvalue involved (I am passing a reference to object, that already exists on the stack.)
int*
和 const int*
是不同的东西.当您将 int*
类型的 a
传递给 function(const int*&)
时,它需要隐式转换为 constint*
首先,它是临时的,即右值,不能绑定到非常量引用.这就是编译器抱怨的原因.
int*
and const int*
are different things. When you pass a
of type int*
to function(const int*&)
, it need to be implicitly casted to const int*
firstly, which is temporary, i.e. rvalue, and couldn't be bound to non-const referece. That's why compiler complains.
问题是,我怎样才能正确地做到这一点?
Question is, how can I do it properly?
您可以更改 a
的类型或 function()
的参数类型以使其完全匹配(可能是 const int*
如果你不会改变指针指向的值),以避免隐式转换和临时变量.或者按照@TartanLlama 的建议,从 function()
返回指针的新值.
You could change the type of a
or the parameter type of function()
to make them match exactly (might be const int*
if you won't change the value pointed by the pointer), to avoid the implicit conversion and temporary variable. Or as @TartanLlama suggested, return the new value of pointer from function()
.
这篇关于对指向 const 对象的非常量指针的非常量引用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!