c++ chi mi spiega qualcosa?
Inviato: 11 gen 2010, 16:14
sto studiando un po' di c++, e mi ritrovo ad affrontare il seguente passo:
il che secondo me' significa che con
const int * pOne;
una volta che assegno il valore a *pOne, poi non posso piu' cambiarlo perche' e' dichiarato costante.
posso invece assegnare un nuovo valore a pOne in' un' altra area di memoria.
mentre con
int * const pTwo;
posso assegnare a *pTwo valori per tante volte quante voglio
non posso invece riassegnare a pTwo una nuova area di memoria
se fosse giusta quest' interpretazione, mi chiedo allora, col compilatore gcc 4.4.1 , che a vdr dobbiamo applicare la const patch e quindi (ho estratto un paio di righe da tale patch)
- char *macroTITLE = strstr(Timer->File(), TIMERMACRO_TITLE);
- char *macroEPISODE = strstr(Timer->File(), TIMERMACRO_EPISODE);
+ const char *macroTITLE = strstr(Timer->File(), TIMERMACRO_TITLE);
+ const char *macroEPISODE = strstr(Timer->File(), TIMERMACRO_EPISODE);
*macroTITLE e *macroEPISODE diventano dei valori costanti e non piu' cambiabili ???
o visto che il testo che sto' leggendo e' targato anno 1997, e' cambiato qualcosa nel frattempo?
Codice: Seleziona tutto
You can use the keyword const for pointers before the type, after the type, or in both places. For example, all of the following are legal declarations:
const int * pOne;
int * const pTwo;
const int * const pThree;
pOne is a pointer to a constant integer. The value that is pointed to can't be changed.
pTwo is a constant pointer to an integer. The integer can be changed, but pTwo can't point to anything else.
pThree is a constant pointer to a constant integer. The value that is pointed to can't be changed, and pThree can't be changed to point to anything else.
The trick to keeping this straight is to look to the right of the keyword const to find out what is being declared constant. If the type is to the right of the keyword, it is the value that is constant. If the variable is to the right of the keyword const, it is the pointer variable itself that is constant.
const int * p1; // the int pointed to is constant
int * const p2; // p2 is constant, it can't point to anything else
const int * pOne;
una volta che assegno il valore a *pOne, poi non posso piu' cambiarlo perche' e' dichiarato costante.
posso invece assegnare un nuovo valore a pOne in' un' altra area di memoria.
mentre con
int * const pTwo;
posso assegnare a *pTwo valori per tante volte quante voglio
non posso invece riassegnare a pTwo una nuova area di memoria
se fosse giusta quest' interpretazione, mi chiedo allora, col compilatore gcc 4.4.1 , che a vdr dobbiamo applicare la const patch e quindi (ho estratto un paio di righe da tale patch)
- char *macroTITLE = strstr(Timer->File(), TIMERMACRO_TITLE);
- char *macroEPISODE = strstr(Timer->File(), TIMERMACRO_EPISODE);
+ const char *macroTITLE = strstr(Timer->File(), TIMERMACRO_TITLE);
+ const char *macroEPISODE = strstr(Timer->File(), TIMERMACRO_EPISODE);
*macroTITLE e *macroEPISODE diventano dei valori costanti e non piu' cambiabili ???
o visto che il testo che sto' leggendo e' targato anno 1997, e' cambiato qualcosa nel frattempo?