0

I am trying to understand this const and pointers.

typedef cu::Op const * operation_t; 
typedef operation_t const * const_operation_ptr_t;

Is operation_t a pointer to a const cu::Op object?

And so on the second line, if you substitute operation_t you get cu::Op const * const *.

Does this mean that const_operation_ptr_t is a constant pointer (its address cannot change) which is pointing to a constant object whose value cannot be changed?

danronmoon
  • 3,814
  • 5
  • 34
  • 56
brewdogNA
  • 25
  • 5
  • BTW: `using operation_t= cu::Op const *;` looks more readable for me as the outdated C syntax. And using the normal ordering of qualifiers make it more readable: `using operation_t= const cu::Op *;` – Klaus Nov 19 '21 at 09:19
  • Some told me, read it right to left. So it is: a (non-const) pointer to const operation_t – Todd Wong Nov 20 '21 at 06:52

1 Answers1

0

The typedef name operation_t denotes a non-constant pointer to a constant object of the type cu::Op.

typedef cu::Op const * operation_t; 

The typedef name const_operation_ptr_t denotes a non-constant pointer to a constant pointer of the type cu::Op const *.

typedef operation_t const * const_operation_ptr_t;

To make the both introduced pointer types as types of constant pointers you need to write

typedef cu::Op const * const operation_t; 

and

typedef operation_t const * const const_operation_ptr_t;

A more simple example. These two declarations

const int *p;

and

int const *p;

are equivalent and declare a non-constant pointer to an object of the type const int.

This declaration

int * const p = &x;

declares a constant pointer to a non-constant object of the type int.

These declarations

const int * const p = &x;

and

int const * const p = &x;

are equivalent and declare a constant pointer to a constant object of the type const int.

Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335