| 1 |
1. Too many consts? |
| 2 |
|
| 3 |
|
| 4 |
-------------------------------------------------------------------------- |
| 5 |
|
| 6 |
1. Too many consts? |
| 7 |
|
| 8 |
> int compareWrapper(void const *p1, void const *p2) |
| 9 |
> { |
| 10 |
> return |
| 11 |
> Person::compare |
| 12 |
> ( |
| 13 |
> static_cast<Person const *const *>(p1), |
| 14 |
> static_cast<Person const *const *>(p2) |
| 15 |
> ); |
| 16 |
> } |
| 17 |
|
| 18 |
> Isn't it const *const * one const * too much? |
| 19 |
|
| 20 |
To answer your question: well, you could omit one const, but it blurrs the |
| 21 |
intention. Person::compare's prototype is |
| 22 |
|
| 23 |
int Person::compare(Person const *const *p1, Person const *const *p2); |
| 24 |
|
| 25 |
which mentions two consts: the parameters are pointers to pointers should not |
| 26 |
be modified nor should be modified what these latter pointers point to. |
| 27 |
|
| 28 |
Hence two consts in the static cast. The last const matches the const in void |
| 29 |
const *, and the first const matches the intention of Person::compare's |
| 30 |
function. Leave out the last const and the compiler complains, leave out the |
| 31 |
first const and the compiler will pass a non-const pointer to a const * |
| 32 |
parameter, which by itself is OK, but blurrs your intention. |