【问题标题】:Non-const reference to a non-const pointer pointing to the const object对指向 const 对象的非常量指针的非常量引用
【发布时间】:2016-07-18 01:23:26
【问题描述】:

简单来说,我有一个简单的指针:

int* a;

现在,我想改变这个指针的值。我想在一个函数中做到这一点。函数保证,它不会改变指针指向的对象,但会改变指针本身。这就是为什么我希望这个函数采用如下参数:非 const 引用(因为指针的值将被更改)指向非 const 指针(指针本身可以更改)指向 const 对象(函数保证,该对象,指针指向的不会改变)。

最简单的函数是:

void function(const int*& a){
    a = 0;
}

但是当我尝试调用这个函数时:

int main(){
    int* a;
    function(a);
    return 0;
}

编译器不高兴,说:

从“const int*”类型的右值初始化“const int*&”类型的非常量引用无效 函数(a);

我不太明白这个错误,因为对我来说没有涉及右值(我正在传递对对象的引用,该对象已经存在于堆栈中。)

问题是,我怎样才能正确地做到这一点?

示例可以在这里找到:https://ideone.com/D45Cid


编辑:

有人建议,我的问题类似于Why isn't it legal to convert "pointer to pointer to non-const" to a "pointer to pointer to const"

我的问题不同,因为我不使用指向指针的指针我只使用指向对象/值的指针并存储对它的引用,因此情况类似于该问题的答案:

const char c = 'c';
char* pc;
const char** pcc = &pc;   // not allowed
*pcc = &c;
*pc = 'C';                // would allow to modify a const object

在我的情况下是不可能的,因为我无法取消引用顶级指针(我没有这样的指针)。

此外,我对这个问题的良好和干净的解决方案提出了质疑,这个问题没有涉及

【问题讨论】:

  • 我不确定您是否可以混淆指针和引用。嗯,你应该是 c++,但请不要。没有人会理解你的代码!
  • @Holt 我不能这样做。这是一个简单的例子,但是在后面的实际代码中我正在对这个对象进行操作,所以我不能将指针声明为 const
  • @hr0m 指向指针的指针比指向引用的指针更好?在我看来,这些有不同的含义。
  • @hr0m 我看不出对指针的引用有什么问题。如果你想接收一个指针并修改它,那么对指针的引用是这样做的自然方法。将指针指向要修改的指针意味着它可能为空,而引用则不是这样。
  • @hr0m:仅仅因为你不熟悉某事并不意味着这是不好的做法。指针的引用是任何 C++ 程序员都应该真正理解的东西。特别是,我不明白为什么双指针应该更容易理解。

标签: c++ pointers reference


【解决方案1】:

我不太明白这个错误,因为对我来说没有涉及右值(我正在传递对对象的引用,该对象已经存在于堆栈中。)

int*const int* 是不同的东西。当您将int* 类型的a 传递给function(const int*&) 时,首先需要将其隐式转换为const int*,这是临时的,即右值,并且不能绑定到非常量引用。这就是编译器抱怨的原因。

问题是,我怎样才能正确地做到这一点?

您可以更改a 的类型或function() 的参数类型以使它们完全匹配(如果您不更改指针指向的值,可能是const int*),以避免隐式转换和临时变量。或者按照@TartanLlama 的建议,从function() 返回指针的新值。

【讨论】:

  • 另一种选择是返回指针的新值,如果已经有返回值,可能在结构或对中。
【解决方案2】:

我不太确定你想要达到什么目的。

不过,这段代码可能会对您有所帮助。 它应该指出你可以如何做你想做的事。

#include <iostream>

using namespace std;

int A = 1;
int B = 2;
int C = 3;

void change_pointer(int*& a){
    // your pointer will point to B
    a = &B;
}

void change_value(int* const& a) {
    // the reference to pointer is constant, but not the value
    // a=&C; wouldn't work
    *a = C;
}

int main(){
    int* a;
    // at this point a is an undefined pointer to an int
    // *a is unallocated space

    a=&A; // you initialize the pointer with an other pointer
    cout << "*a = " << *a << ", A = " << A << ", B = " << B << ", C = " << C << endl;

    change_pointer(a); // makes 'a' point to B
    cout << "*a = " << *a << ", A = " << A << ", B = " << B << ", C = " << C << endl;

    change_value(a); // changes the value pointed by a to C (in the process modifying the value of B)
    cout << "*a = " << *a << ", A = " << A << ", B = " << B << ", C = " << C << endl;

    return *a;
}

编辑: 回应 TartanLlama 的评论。

我能看到使用“非 const ref”到“非 const 指针”到“const int”的唯一方法是使用typedef

#include <iostream>

using namespace std;

typedef const int const_int_t;

const_int_t A = 1;
const_int_t B = 2;

void change_pointer(const_int_t*& a){
    // your pointer will point to B
    a = &B;
}

int main(){
    const_int_t* a;

    a=&A; // you initialize the pointer with an other pointer
    cout << "*a = " << *a << ", A = " << A << ", B = " << B << endl;

    change_pointer(a); // makes 'a' point to B
    cout << "*a = " << *a << ", A = " << A << ", B = " << B << endl;

    return *a;
}

【讨论】:

  • 这不是 OP 想要的。他想要对指向 const int 的非常量指针的非常量引用。
  • @TartanLlama:感谢您为我清理问题。我使用 typedef 添加了一个解决方案。
  • 这也不是他想要的,typedef 也没有添加任何东西。问题是从非常量指针到非常量 int 的非常量指针指向 const int 的非常量引用。
猜你喜欢
  • 2021-06-19
  • 1970-01-01
  • 2015-02-08
  • 1970-01-01
  • 2021-04-19
  • 2019-02-05
  • 1970-01-01
  • 1970-01-01
  • 2011-01-31
相关资源
最近更新 更多