Quick const pointer question...

Started by
6 comments, last by JohnBolton 18 years, 1 month ago
Evening all. Say I have a function in a class which creates an instance of an object using the new keyword, stores the pointer by some means (in a map or a vector or something), and returns a const pointer to the object (the return type of the function being const [ClassName] *). What happens if delete is called on the returned pointer? Is the instance of the object it points to destroyed, thereby rendering the pointer held by the class which created it invalid? I assume yes, but I was just wondering if someone can clarify this. Also, if the answer is yes, I assume that I should use something like boost::shared_ptr to avoid instances of objects being destroyed accidentally? Thanks in advance...
My opinion is a recombination and regurgitation of the opinions of those around me. I bring nothing new to the table, and as such, can be safely ignored.[ Useful things - Firefox | GLee | Boost | DevIL ]
Advertisement
Yes and yes, or use a reference or non-owning pointer wrapper.

Σnigma
Actually deleting a const pointer is not legal in C++ so it shouldn't be a problem unless you're using a non-conforming compiler.

- Anonymous coward.
deleting a const pointer IS legal.

If it weren't, this line would always result in a memory leak:
const int* i = new int;
Quote:C++ Standard, Final Draft, Section 5.3.5, Paragraph 2
If the operand has a class type, the operand is converted to a pointer type by calling the above-mentioned conversion function, and the converted operand is used in place of the original operand for the remainder of this section. In either alternative, if the value of the operand of delete is the null pointer the operation has no effect. In the first alternative (delete object), the value of the operand of delete shall be a pointer to a non-array object created by a new-expression, or a pointer to a subobject (1.7) representing a base class of such an object (10). If not, the behavior is undefined. In the second alternative (delete array), the value of the operand of delete shall be the pointer value which resulted from a previous array new-expression.68) If not, the behavior is undefined. [Note: this means that the syntax of the delete-expression must match the type of the object allocated by new, not the syntax of the new-expression. ] [Note: a pointer to a const type can be the operand of a delete-expression; it is not necessary to cast away the constness (5.2.11) of the pointer expression before it is used as the operand of the delete-expression. ]

See the final note.

Σnigma
const int* means that the value of the pointer can't change.
int const* means that what is being pointed to can't be changed but the value of the pointer can.

const int const* means that neither can be changed, this is completely const.

I believe this to be correct.

Dave
Nearly.

const int * - pointer to an int which is const. The pointer can point at other things but whatever it's pointed at can't change.
int const * - same as above, pointer to an int which is const. The pointer can point at other things but whatever it's pointed at can't change.
int * const - const pointer to an int. The pointer can't point at anything else, what it points at can change.
const int * const - const pointer to an int which is const. Neither the pointer nor the object it points to can change.
int const * const - same as above, const pointer to an int which is const. Neither the pointer nor the object it points to can change.

Σnigma
Quote:Original post by iNsAn1tY
... What happens if delete is called on the returned pointer? Is the instance of the object it points to destroyed, thereby rendering the pointer held by the class which created it invalid?


delete destroys the object that the pointer points to. It does not affect the pointer itself or any other pointers that point to the destroyed object. boost::shared_ptr is a way to avoid accidental deletions, but it is not necessarily always the best way. When the lifetime of an object is well-defined, careful control of its construction and destruction is generally better than using boost::shared_ptr.
John BoltonLocomotive Games (THQ)Current Project: Destroy All Humans (Wii). IN STORES NOW!

This topic is closed to new replies.

Advertisement