Click here to Skip to main content
15,890,741 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: reference to const int Pin
jschell11-May-12 13:21
jschell11-May-12 13:21 
AnswerRe: reference to const int Pin
CPallini9-May-12 22:53
mveCPallini9-May-12 22:53 
AnswerRe: reference to const int - more Pin
Richard MacCutchan10-May-12 0:32
mveRichard MacCutchan10-May-12 0:32 
GeneralRe: reference to const int - more Pin
Aabid10-May-12 1:06
Aabid10-May-12 1:06 
GeneralRe: reference to const int - more Pin
Richard MacCutchan10-May-12 1:12
mveRichard MacCutchan10-May-12 1:12 
GeneralRe: reference to const int - more Pin
Aabid10-May-12 1:36
Aabid10-May-12 1:36 
GeneralRe: reference to const int - more Pin
Richard MacCutchan10-May-12 2:23
mveRichard MacCutchan10-May-12 2:23 
AnswerRe: reference to const int Pin
Aescleal10-May-12 4:56
Aescleal10-May-12 4:56 
If you take the address or reference of a const object and assign it to a non-const pointer or reference and you then modify the value through that pointer or reference the compiler can do whatever it wants. Even laugh at you or fire nuclear missiles (if it's got any) while twirling it's moustache.

In C if you declare a constant it's just a read only variable. It has it's own memory location, everything's peachy and works the way you expect. In C++ it really is a constant for the rest of time. It'll never change. Wherever the compiler sees i it can (and probably does) just hardcode the value 10.

So what happens when you bind a non-const reference to a const value? Well anything can. But in your compiler's case it cooks up an anonymous temporary variable with the same lifetime as the reference and assigns whatever the constant is to it. This also happens when you take the address of a const as well and assign it to a non-const pointer.

What's actually happening is something like:

C++
int x = 10;

cout << "i= " << 10 << endl;
cout << "j= " << x << endl;
cout << "&i= " << &x << endl;
cout << "&j= " << &x << endl;

x++;

cout << "j= " << x << endl;
cout << "i= " << 10 << endl;


The only way you're going to change i is to hunt through the executable code and change the immediate value there Smile | :)

Incidentally how this happens is non-portable, it happens on VC++ and GCC but other compilers might have slightly different takes on it.

modified 10-May-12 11:51am.

AnswerRe: reference to const int Pin
jschell10-May-12 9:23
jschell10-May-12 9:23 
Questionuse mfc open "*.ppt"--can "use" the ppt Pin
wangafei9-May-12 21:07
wangafei9-May-12 21:07 
AnswerRe: use mfc open "*.ppt"--can "use" the ppt Pin
ThatsAlok9-May-12 21:08
ThatsAlok9-May-12 21:08 
GeneralRe: use mfc open "*.ppt"--can "use" the ppt Pin
wangafei9-May-12 21:13
wangafei9-May-12 21:13 
AnswerRe: use mfc open "*.ppt"--can "use" the ppt Pin
David Crow10-May-12 2:37
David Crow10-May-12 2:37 
AnswerRe: use mfc open "*.ppt"--can "use" the ppt Pin
Malli_S10-May-12 0:55
Malli_S10-May-12 0:55 
Questioncreating a dialog in non mfc application Pin
Rajeev.Goutham9-May-12 21:01
Rajeev.Goutham9-May-12 21:01 
AnswerRe: creating a dialog in non mfc application Pin
ThatsAlok9-May-12 21:11
ThatsAlok9-May-12 21:11 
GeneralRe: creating a dialog in non mfc application Pin
wangafei9-May-12 21:15
wangafei9-May-12 21:15 
GeneralRe: creating a dialog in non mfc application Pin
Rajeev.Goutham9-May-12 21:22
Rajeev.Goutham9-May-12 21:22 
AnswerRe: creating a dialog in non mfc application Pin
ThatsAlok9-May-12 22:57
ThatsAlok9-May-12 22:57 
GeneralRe: creating a dialog in non mfc application Pin
wangafei10-May-12 16:40
wangafei10-May-12 16:40 
AnswerRe: creating a dialog in non mfc application Pin
ThatsAlok10-May-12 19:57
ThatsAlok10-May-12 19:57 
GeneralRe: creating a dialog in non mfc application Pin
Rajeev.Goutham9-May-12 21:24
Rajeev.Goutham9-May-12 21:24 
QuestionDrawimage Pin
john56329-May-12 2:09
john56329-May-12 2:09 
AnswerRe: Drawimage Pin
Anu_Bala9-May-12 2:19
Anu_Bala9-May-12 2:19 
GeneralMessage Removed Pin
9-May-12 2:28
john56329-May-12 2:28 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.