Muhammad Hewedy
Muhammad Hewedy

Reputation: 30058

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

Upvotes: 17

Views: 15046

Answers (6)

James Kanze
James Kanze

Reputation: 153909

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.

Upvotes: 16

Sarfaraz Nawaz
Sarfaraz Nawaz

Reputation: 361312

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.

Upvotes: 5

sehe
sehe

Reputation: 392893

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

Upvotes: 19

Martin B
Martin B

Reputation: 24140

Yes, const in front of a reference means that the object referenced through it is treated as const (i.e. 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.

Upvotes: 6

unwind
unwind

Reputation: 399753

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.

Upvotes: 6

thiton
thiton

Reputation: 36049

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.

Upvotes: 4

Related Questions