views:

467

answers:

10

I am trying to learn C++ via some web tutorials. I don't have a compiler available to me, otherwise I would try this out. I'm not sure what is meant by a const pointer. Does that just mean it always points to the same memory address? Why would you ever want to do that? Would the following code be legal?

...
int * const aPointer = new int;
... //do something with aPointer
delete aPointer;
... //do something else, including possibly more 'new' statements
aPointer = new int;
...
+1  A: 

A const pointer means that you can change the value of the variable which is being pointed to, but you can't change where the pointer is pointed. I don't use them often myself, but a common application for const pointers is in defining specific memory segments that you need to address. See this question for more information.

As an aside, you should try to get a compiler on your computer if you can. I've shown myself many times that human brains are poor C++ compilers.

James Thompson
+4  A: 

You can't learn to drive a car just by reading books.

Get yourself a C++ compiler if you want to learn C++. g++ is free of charge, as well as Visual Studio 2008 Express Edition.

As for your question, a const pointer is a zone of memory that is ready only. Example: A class may provide read-only access to an internal buffer.

Note that you also have the const pointer that is also const, aka

const char * const p

In that case, even the value of the pointer cannot be modified.

Edouard A.
This post is confusing and doesn't distinguish clearly between when the value is const and when the pointer is const.
Drew Hoskins
It takes more than reading a post to clearly understand the notion anyway.
Edouard A.
You can't learn the traffic laws just by driving, either. You won't learn how to program without programming, and you won't learn what's legal and reliable without reading books.
David Thornley
To follow up on this image, you certainly know that to be a good driver, it's less important to know the rules than to be able to anticipate what other drivers will do and control your own vehicle. We're not doing computer science here, but engineering.
Edouard A.
A: 

In your code, the pointer cannot move, but the data pointed to can change.

It's legal up to the first delete. A subsequent new would not work because it's an assignment to a constant.

It's relatively unusual to see this, more common is to see where the data pointed to is unchangeable but the pointer can move.

int bar;
int baz;
const int * foo = &bar;
*foo = 4; // illegal
foo = &baz; // legal

having both pointer and value being const is common with strings

const wchar_t * const myString = L"String that will never change.";
Drew Hoskins
+9  A: 

Const pointer could mean a few different things. I recommend checking out the C++ FAQ Lite on the matter.

You can have:

const int* p;
int* const p;
const int* const p;

All three mean different things. Yes, it's kind of confusing.

In your case, you have the second, which means you have a constant pointer to a non-constant object. That is, you can change the value of the integer via the pointer, but you can't change what the pointer points to. So the code you posted would not be legal.

Evan Shaw
+15  A: 

A simple way to remember how const works with pointers is to remember that it always applies to whatever is to the left of it, unless it's the left-most keyword, in which case it applies to the right.

Examples:

Pointer to a constant char: The pointer can be changed to point to something else, but the char it initally points to cannot change value.

const char * p;

Constant pointer to a char: The pointer cannot be changed to point to anything else, but the char it points to can change value.

char *const p;

Constant pointer to a constant char: The pointer cannot be changed to point to anything else, and the char it points to cannot change value.

const char *const p;
Jeff L
Second and third examples have a misplaced const. You mean char * const p;
Drew Hoskins
Thanks for catching that.
Jeff L
+1  A: 

Your code is not legal. You can't assign to aPointer (except using copy-style initialisation, which in fact is not assignment even though it looks like it) if aPointer is declared const like that.

But usually when people say "a const pointer", they mean const int * aPointer, not int * const aPointer as you have in your code. The whole internet will explain the difference at the drop of a hat. As far as I know, the term "const pointer" isn't defined in the standard, so we're free to do this even though it's potentially confusing. "Pointer-to-const" would be an unambiguous description, and a pointer-to-const is much more commonly used than a pointer-which-is-itself-const.

A pointer-which-is-itself-const is used to refer to something, where you won't want the pointer to refer to a different thing at any point in its life. For instance, this is a pointer-which-is-itself-const, because "this object" remains the same object through the execution of a member function. The C++ language has opted not to let you decide part way through that you want to assign some other value to this, to make it refer to some other object. In C++ references often serve that purpose too, since they cannot be "re-seated" to change the referand.

Steve Jessop
A: 

Since you're new to C++, for the answer to this question and many other questions you may have or don't know you have, check out the C++ FAQ

Shing Yip
A: 

I don't have a compiler available to me, otherwise I would try this out

Everyone has a C++ Compiler available to them: http://www.online-compiler.com

There are many others, but this seems to work...

Roddy
+2  A: 

G'day,

To remember this easily you can use the trick that Scott Meyers describes in his excellent book "Effective C++" (sanitised Amazon link)

You draw a line through the declaration where the asterisk is located.

  • If the keyword const appears to the left of the line, then you can't change the value of the item that you're pointing to.
  • If the keyword const appears to the right of the line, then you can't change the pointer to point to another location.
  • If const appears on both sides, then you can't change the pointer and you can't change the value of what you're pointing to.

BTW That book is excellent, and while not for a beginner, is definitely a way of taking your C++ knowledge to the next level! Highly recommended.

HTH

cheers,

Rob Wells
When teaching C, I referred to it as a "cover-up" rule.
David Thornley
These rules (and that book) are excellent.
James Thompson
A: 

As it has already been pointed out the perhaps most common const pointer is

const char* p;

The variable p can change, but the data p points to is unmodifable.

However, moving the const keyword to the left of the asterisk does not alter the meaning of the declaration:

char const* p;

I prefer the later since it becomes much easier to remember where to place the const keywords when declaring const pointers to const pointers:

char const* const* p;

Again, the variable p can change and the data pointed to is unmodifiable. Furthermore, the data is declared as const pointers meaning that it points to data that cannot be modified.

The more common notation for this type is

const char* const* p;

Placing the const keyword immediately to the left of the asterisk it modifies (or ampersand for reference) makes it easy to create complex types involving the const keyword. For example, a pointer to const pointers:

char const** p;

and a const pointer to pointers:

char* const* p;

Remember to "read" pointer declarations from the right to the left, and don't declare more than one pointer in each statement to avoid a lot of confusion.

Martin Liversage