简体   繁体   中英

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?

#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:

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; 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.

References cannot be changed, after they've been initialized. So it doesn't make sense to talk about a "const reference". 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).

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.

A reference is neither const nor non-const, it is just a reference. You can't change the referee of a reference (that's its reason d'etre), so talking about constness makes no sense. In your example, the reference is just called a const reference because its refers to a const type.

const keyword at a front of a references means Const Reference to const value, correct?

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.

  • But if the object is unmodifiable, then attempting to modify it by casting away the const-ness, would invoke undefined behavior.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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