mardi 1 septembre 2015

c++ Why low level const important when doing assignment?

I have the following codes:

const int v2 = 0; int v1 = v2;                 // v2 - top level const
int *p1 = &v1, &r1 = v1;
const int *p2 = &v2, *const p3 = &i, &r2 = v2; // p2 - low level const
                                                  p3 - top level const & low level const
                                                  r2 - low level const

I do understand the top and low level const.

Question:
Why p1 = p2 failed?

According to C++ Primer 5th Ed (Stanley Lipmann) p2 const is low level and hence cannot be ignored. Assignment of p2 low level const to p1 which is int * failed.

But if i think hard, assigning to p1 doesn't change object p2. So what does it matter if p1 is int * and the p2 has low level const? Just doesn't make sense to me.

Aucun commentaire:

Enregistrer un commentaire