【问题标题】:const pointers vs const references in C++C++ 中的 const 指针与 const 引用
【发布时间】:2012-06-24 20:49:26
【问题描述】:

根据下面的程序,我可以理解,引用前面的const关键字表示Const Reference to const value,对吗?

#include <iostream>
using namespace std;

struct s
{
    int x;
};

int main(void)
{
    s a = {10}, b = {30};

    // IN POINTERS ----------------
    const s* ptrToConstValue;
    ptrToConstValue= &a;
    //ptrToConstValue->x = 30; 
    ptrToConstValue = &b;

    s* const constPtrToNonConstVaue = &a;
    constPtrToNonConstVaue->x = 40;
    //constPtrToNonConstVaue = &b;

    const s* const constPtrToConstValue = &a;
    //constPtrToConstValue = &b;
    //constPtrToConstValue->x = 30;


    // IN REFERENCES -------------
    const s& seemsToBeConstRefToConstValue = a;
    //s = b;
    //s.x = 30;

    return 0;
}

【问题讨论】:

  • 如果你愿意,它总是被解析为“(const T)&amp;”,而不是“const (T&amp;)”。

标签: c++ reference


【解决方案1】:

所以困惑是这样的:

X x;

X* px       = &x; // pointer to x
X* const px  = &x; // *const pointer* to x

const X* px   = &x; // pointer to *const x*
X const* px   = &x; // identical

const X* const px = &x; // *const pointer* to *const x*

现在作为参考,“指针部分”始终是 const:

X& rx = x;       // ref to x

X const& rx = x; // ref to const x
const X& rx = x; // identical

【讨论】:

    【解决方案2】:

    引用总是常量,所以你不需要 const 关键字 他们;事实上,这是被禁止的。

    所以你有:

    S*                ps;   //  Non-const pointer to a non-const value
    S const*          psc;  //  Non-const pointer to a const value
    S* const          pcs;  //  Const pointer to a non-const value
    S const* const    pcsc; //  Const pointer to a const value
    

    ,但仅限于:

    S&                rs;   //  (const) reference to a non-const value
    S const&          rsc;  //  (const) reference to a const value
    

    紧跟在类型名称后面的const 可以移动 到声明的开头,以一些混乱为代价 读者。

    【讨论】:

    • 奇怪的是,整个移动 const 以跟随类型让我感到困惑。我想这是因为它似乎是一种比我学习时更近的编码风格。我习惯于看到 const R&,我认为这种风格更常见。但是我可以理解为什么你上面的风格更容易理解,因为它遵循从右到左的阅读方式
    • @Firedragon 背后的动机有两个。首先,当然,在大多数情况下,您别无选择:const 必须遵循 const。第二个是typedef会发生什么:如果你有typedef char* pc;之类的东西,写const pc,什么是const。我也是在合法的时候第一次看到左边的const;是 typedef 参数说服了我。
    【解决方案3】:

    引用在初始化后无法更改。因此,谈论“const 引用”是没有意义的。引用几乎值,在这种情况下,值是恒定的,也不能更改。

    【讨论】:

    • "References cannot be changed after they have been initialized" ,你在说什么?这编译得很好: void bar(Box& a) { a.data = 33; a = 盒子(100); }
    【解决方案4】:

    是的,引用前面的const 表示通过它引用的对象被视为 const(即您不能访问非 const 方法或数据成员)。

    无论如何,引用本身始终是“const”,因为一旦它被初始化,您就无法修改它以引用不同的对象。

    【讨论】:

    • 也许在这里“价值”比“对象”更好?
    【解决方案5】:

    引用既不是 const 也不是非常量,它只是一个引用。你不能改变引用的裁判(这就是它存在的原因),所以谈论 constness 是没有意义的。在您的示例中,引用仅称为 const 引用,因为它引用了 const 类型。

    【讨论】:

      【解决方案6】:

      引用前面的 const 关键字表示 Const 对 const 值的引用,对吗?

      这意味着您不能使用引用更改对象。

      引用所指的对象仍然可以修改,但不能使用引用修改:

      • 如果它所引用的对象确实是可修改的,那么您可以使用const_cast 抛弃常量,然后修改该对象。

      • 但是,如果对象是不可修改的,那么尝试通过丢弃 const 来修改它,将调用未定义的行为。

      【讨论】:

      • +1 for "using the reference",其他答案看起来好像对象本身突然变得恒定,只是因为您使用了 const 引用。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-11-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多