1

Possible Duplicate:
what is the difference between const int*, const int * const, int const *

What is the difference between

A const * pa2 = pa1;

and

A * const pa2 = pa1;

(I have some class A for example).

Community
  • 1
  • 1
lego69
  • 767
  • 1
  • 12
  • 20
  • 4
    Duplicate of [what is the difference between const int*, const int * const, int const *](http://stackoverflow.com/questions/1143262/what-is-the-difference-between-const-int-const-int-const-int-const) (I knew this had been asked before, it just took me a while to find it :-P) – James McNellis Jun 13 '10 at 20:44

3 Answers3

3

Read the type from right to left:

A const * pa2 = pa1;

pa2 is a pointer to a read-only A (the object may not be changed through the pointer)

A * const pa2 = pa1;

pa2 is a read-only pointer to A (the pointer may not be changed)

This does not mean that A cannot change (or is actually constant) const is misleading, understand it always as read-only. Other aliased pointers might modify A.

jdehaan
  • 19,700
  • 6
  • 57
  • 97
3
A const * pa2

This is a non-const pointer to a const A. You can change where the pointer points but you can't change the object pointed to by the pointer.

A * const pa2

This is a const pointer to a non-const A. You can't change where the pointer points but you can change the object pointed to by the pointer.

A const * const pa2

This is a const pointer to a const A. You can't change where the pointer points and you can't change the object pointed to by the pointer.

You may find the "Clockwise/Spiral Rule" helpful when trying to decipher declarations in C and C++.

James McNellis
  • 348,265
  • 75
  • 913
  • 977
0

It means that the first is a pointer to a const object, which (loosely) means the object can't be change.

The second is a const pointer to an object, which means the pointer itself can not be changed (ie assigned to a different object).

Alan
  • 45,915
  • 17
  • 113
  • 134