Re: more related to const
pavan734@gmail.com wrote:
Murali Krishna wrote:
Can you plz send the complete code?
-- Murali Krishna
I dont know much abt it but here is a code which i have checked
/***a.h***/
extern const unsigned int& temp ;
/****end of a.h***/
/***a.cpp***/
unsigned int temp2 ;
int main(){
temp2 = 20 ;
cout << "\ntemp =" << temp ; //prints 20
temp2 = 40 ;
cout << "\ntemp =" << temp ; //prints 40
} //end of main
const unsigned int& temp =temp2 ;
/****end of a.cpp***/
Main problem is even though we have made temp to be const, it will
alter if temp2 is changed.
you asked in the previous reply..
But is there any way where we can declare a const global
variable & assign it later.
This has nothing to with the above code. I'll try to explain breifly.
reference variable creates an alias to another variable of same type.
so if you say..
int i = 10;
int& ref = i;
variables ref and i are same. meaning they both hold same address.
unsigned int temp2 ;
is a normal auto storage class variable. Change it when ever you want.
const unsigned int& temp =temp2 ;
temp is const alias of temp2. so.. what's the point?
temp always has to be alias of temp2. This is what const ref mean.
try to reference to another variable. it will give an error.
So you did not change value of const variable. and you cannot change
reference alias also.
const type& is different from const type.
-- Murali Krishna