Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ const used twice in static array declaration

I have seen const used twice in the declaration of a static array before and now that I am creating my own static array I am wondering why const would be needed twice in some situations.

Does having an array of pointers make a difference?

a. static const TYPE name[5];
b. static const TYPE const name[5];

c. static const TYPE* name[5];
d. static const TYPE* const name[5];

My understanding is that b. is invalid, but if using const twice is valid, what is its purpose?

like image 570
Jim Avatar asked Aug 23 '12 22:08

Jim


1 Answers

const TYPE* x;

Means that the thing that x points at is const.

TYPE* const x;

Means that the pointer x is const.

Combining the 2 you get:

const TYPE* const x;

Meaning the pointer and the thing pointed to are both const.

like image 146
anio Avatar answered Nov 13 '22 05:11

anio