views:

109

answers:

4

Hello
i have a cpp code wich i'm having trouble reading. a class B is defined
now, i understand the first two lines, but the rest isn't clear enough.
is the line "B const * pa2 = pa1" defines a const variable of type class B?
if so, what does the next line do?

B a2(2);
B *pa1 = new B(a2);
B const * pa2 = pa1;
B const * const pa3 = pa2;

also, i'm having trouble figuring out the difference between these two:

char const *cst = “abc”;
const int ci = 15;


thank you

A: 

The second code snippet shows a way to change the order of the type and the const without making anything different: The order is irrelevant.

In the first code snippet, the third variable's "const" signifies that you cannot change the object pointed to using that pointer. In the same code snippet the last variable's other const signifies that you cannot reseat the pointer to point to another object afterwards. It's, for that matter, a constant variable and a pointer that doesn't allow changing the object pointed to.

To illustrate the matter of the second code snippet using variables of the first code snippet - you may change the order of the type and the "const" in the first code snippet for the third and next variable without making anything different:

const B * pa2 = pa1; // Same
const B * const pa3 = pa2; // Same
Johannes Schaub - litb
+1  A: 

This code declares a pointer to a constant B - in other words, it cannot be used to change the value of what it is pointing to:

B const * pa2 = pa1;

Alternatively, the following code declares a constant pointer to a constant B - so, pa3 cannot be used to change the value of what it is pointing to, and it cannot be modified to point to something else:

B const * const pa3 = pa2;

This page contains an explanation of const pointers.

To address your second question,

char const *cst = “abc”; - Declares a pointer to a constant char - in this case it is the string "abc".

const int ci = 15; - Declares a constant integer 15, which cannot be changed.

Justin Ethier
+3  A: 

Taken from C++ FAQ lite:

You have to read pointer declarations right-to-left.

  • Fred const* p means "p points to a constant Fred": the Fred object can't be changed via p.
  • Fred* const p means "p is a const pointer to a Fred": you can't change the pointer p, but you can change the Fred object via p.
  • Fred const* const p means "p is a constant pointer to a constant Fred": you can't change the pointer p itself, nor can you change the Fred object via p.
Fionn
A: 

The basic rule in C++ is that const applies "constness" (you can't change it) to the part of the object to its left. The exception is the very front of the declaration line. If const is there, it instead applies to the part of the object to its right.

Thus the line:

B const * pa2 = pa1; 

Means "pa2 is a pointer to a constant object of type B, with (the pointer) pa1 assigned as its initial pointer value".

The line:

B const * const pa3 = pa2; 

Means "pa3 is a constant pointer to a constant object of type B. It points to the same object we initialized pa2 to point to."

Many (if not most) folks like to put the const on the front of the line when they can, because they like the way it reads. Others (myself and your code author included) don't like dealing with syntactic exceptions, and prefer to put it to the right of its object.

For much, much more information on this topic see this question: http://stackoverflow.com/questions/988069/in-c-is-const-after-type-id-acceptable

T.E.D.