pointers - Learning C++: why is this illegal? -
i bought c++ primer book few days ago, , started learning new language!
at moment, i'm trying undestand why block wrote myself illegal:
i initialize const int 512; initialize const int const pointer n; doesn't allow me create const int const pointer pointer, using correctly double ** , everything:
const signed int n = 512; const signed int *const npointer = & n; const signed int **const npointer2 = & npointer;
does have simple explanation? time!
in order fix code have 2 options:
//option 1 : const signed int *const npointer = & n; const signed int *const *const npointer2 = & npointer; //option 2 : const signed int * npointer = & n; const signed int **const npointer2 = & npointer;
explanation:
when npointer constant pointer t, npointer2 should point constant pointer t, in first option. alternatively, when npointer2 pointer non-const pointer t, pointee npointer should non-const well.
when part of book define own type aliases, can remove clutter see how these alternatives work follows:
//simplifying notation: using mytype = const signed int *; using mytypec = const signed int * const; //your code equivalent to: mytypec npointer = & n; mytype *const npointer2 = & npointer; //option one: mytypec npointer = & n; mytypec *const npointer2 = & npointer; //option two: mytype npointer = & n; mytype *const npointer2 = & npointer;
Comments
Post a Comment