简体   繁体   English

const指针与C ++中的const引用

[英]const pointers vs const references in C++

According to the following program, I can understand that, const keyword at a front of a references means Const Reference to const value , correct? 根据以下程序,我可以理解,引用前面的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;
}

So the confusion is this: 所以混淆是这样的:

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*

Now in reference, the 'pointer part' is always const: 现在参考,'指针部分'总是const:

X& rx = x;       // ref to x

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

References are always const, so you don't need the const keyword for them; 引用总是const,所以你不需要const关键字; it is, in fact, forbidden. 事实上,它是被禁止的。

So you have: 所以你有了:

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

, but only: , 但只有:

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

The const which immediately follows the name of the type can be moved to the beginning of the declaration, at the cost of some confusion to the reader. 紧跟在类型名称后面的const可以移动到声明的开头,代价是对读者有些困惑。

References cannot be changed, after they've been initialized. 初始化后,引用无法更改。 So it doesn't make sense to talk about a "const reference". 所以谈论“const引用”是没有意义的。 The reference pretty much is the value, and in cases like these the value is constant and cannot be changed, either. 参考几乎就是值,在这种情况下,值是常量,也不能改变。

Yes, const in front of a reference means that the object referenced through it is treated as const (ie you can't access non-const methods or data members). 是的,引用前面的const意味着通过它引用的对象被视为const(即,您不能访问非const方法或数据成员)。

The reference itself is always "const" anyway in the sense that you can't modify it to reference a different object once it has been initialized. 无论如何,引用本身总是“const”,因为在初始化之后,您无法修改它以引用不同的对象。

A reference is neither const nor non-const, it is just a reference. 引用既不是const也不是非const,它只是一个引用。 You can't change the referee of a reference (that's its reason d'etre), so talking about constness makes no sense. 你不能改变参考的裁判(这是它的理由),所以谈论constness是没有意义的。 In your example, the reference is just called a const reference because its refers to a const type. 在您的示例中,引用仅称为const引用,因为它引用了const类型。

const keyword at a front of a references means Const Reference to const value, correct? 引用前面的const关键字表示Const引用const值,对吗?

It means you cannot change the object using the reference. 这意味着您无法使用引用更改对象。

The object which reference is referring to, could still be modifiable, but it cannot be modified using the reference: 引用所引用的对象仍然可以修改,但不能使用引用进行修改:

  • If the object which it refers to, is really modifiable, then you can cast away the const-ness using const_cast and then modify the object. 如果它所引用的对象是可以修改的,那么你可以使用const_cast抛弃const-anss然后修改对象。

  • But if the object is unmodifiable, then attempting to modify it by casting away the const-ness, would invoke undefined behavior. 但是如果对象是不可修改的,那么试图通过抛弃const来修改它,将调用未定义的行为。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM