embedc
embedc

Reputation: 1665

Is it common to declare const pointers in C++?

C++ style guides strongly advise that one should declare objects as const if we don't intend to modify them. It follows that when we declare a pointer to an object/variable that isn't going to be reassigned we should declare it const:

T* const pObject = new T();

It just seems that C++ developers usually don't follow this rule in case of pointers, do they?

Upvotes: 4

Views: 179

Answers (4)

eerorika
eerorika

Reputation: 238401

Is it common to declare const pointers in C++?

It is not quite as common as non-const pointers (not to be confused with pointer to const vs pointer to non-const; just to be clear). Pointers are typically used as an iterator for an array. Iteration requires modification of the iterator.

But it is not unheard of either.

T* const pObject = new T();

Any decent coding style would primarily advice against using a bare owning pointer.

So when we declare pointer to an object/variable that isn't going to be reassigned we should declare it const

It is quite rare for a pointer to not be reassigned at some point. Unless you need the ability to point to null, it is often advisable to use a reference when you don't need reassignment.

Even in the case where you do need a pointer and don't need reassignment, preferring const objects instead of non-const is only marginally useful style guide. It mostly makes significant difference in cases that are now replaced by constexpr (which makes the variable implicitly const). As such, many don't follow, or might not even be aware of such style.

However, feel free to follow the style if you so prefer.

Upvotes: 3

A pointer is an object.

If you wish to prevent modification of the value held by the pointer, i.e. to make it hold only a single address within its lifetime, then yes make it const. The same considerations that apply for any other object type also apply here.

I suspect the reason you may not see it much is that the declarator syntax in C++ kinda makes this uncomfortable to type. Not to mention that whether or not the pointee is const is usually far more important.

Upvotes: 8

Davide Spataro
Davide Spataro

Reputation: 7482

A pointer is just a variable containing an address, but in the end it is a variable like any other.

You should make a pointer const for the same reasons you make a simple int const. So you if have good reasons to have a normal variable const then for the same reasons you should also aim at having const pointers.

It is pretty common (and I would add, good practice) in the C++ community to declare const pointers to const data.

Upvotes: 3

Sathiraumesh
Sathiraumesh

Reputation: 6117

Yeah, it's a good practice because it avoids you from changing your reference unintentionally. It is possible that you can change the value of a variable somewhere in your code not knowingly adding const make sure the variables valued doesn't get changed anywhere.

Upvotes: 4

Related Questions