|
That probably does carry some truth And that's why we love and hate C++
> The problem with computers is that they do what you tell them to do and not what you want them to do. <
> If it doesn't matter, it's antimatter.<
|
|
|
|
|
Responsible adult? You've never worked in the games industry Biggest bunch of hackers (in the bad sense) out there.
|
|
|
|
|
Aescleal wrote: Responsible adult? You've never worked in the games industry Biggest bunch of hackers (in the bad sense) out there.
I do however have extensive telecom and financial experience and the same thing applies there.
|
|
|
|
|
I don't know if (by the C++ specifications) that is the correct behaviour, however both the compiler provided by Visual Studio 2005 and g++ 4.4.3 gave me the same results you obtained.
Apparently (even with optimizations turned off) the compiler replaces each occurrence of i with the literal value 10 , making all the changes you perform on the corrensponding memeory address irrelevant.
Compare the code generated for j and i in the cout lines:
j
cout << "j= " << j << endl
0041143A mov eax,dword ptr [__imp_std::endl (4182B4h)]
0041143F push eax
00411440 mov ecx,dword ptr [j] <=============== MEMORY ADDRESS
00411443 mov edx,dword ptr [ecx]
00411445 push edx
00411446 push offset string "j= " (41570Ch)
0041144B mov eax,dword ptr [__imp_std::cout (4182BCh)]
00411450 push eax
00411451 call std::operator<<<std::char_traits<char> > (4110F0h)
00411456 add esp,8
00411459 mov ecx,eax
0041145B call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (4182C0h)]
00411461 mov ecx,eax
00411463 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (4182C4h)]
i
cout << "i= " << i << endl
00411469 mov eax,dword ptr [__imp_std::endl (4182B4h)]
0041146E push eax
0041146F push 0Ah <================= LITERAL (10)
00411471 push offset string "i= " (415710h)
00411476 mov ecx,dword ptr [__imp_std::cout (4182BCh)]
0041147C push ecx
0041147D call std::operator<<<std::char_traits<char> > (4110F0h)
00411482 add esp,8
00411485 mov ecx,eax
00411487 call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (4182C0h)]
0041148D mov ecx,eax
0041148F call dword ptr [__imp_std::basic_ostream<char,std::char_traits<char> >::operator<< (4182C4h)]
Veni, vidi, vici.
|
|
|
|
|
If you change your reference statement to:
const int& j = i;
you will not be allowed to increment the value of j as it is now a constant integer value and can only ever be 10 .
Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness
|
|
|
|
|
i know that, but i was trying to understand the previous behavior and that is still a mystery!
|
|
|
|
|
Aabid wrote: the previous behavior and that is still a mystery!
Not really, in your original code you had:
const int i = 10;
int& j = (int&)i;
so you are casting away the const -ness of i and effectively making j refer to a new variable, which starts out with the same value as i , but is modifiable since it is not a constant.
Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness
|
|
|
|
|
But reference never creates a new variable, it is an alias to the same memory location, if you see the output of the code, you will see "i" and "j" have the same address.
ok, if the following statement creates a new variable, is there any way to get the address of that variable? i tried to print &(int&)i, it still gives the same address as "i" and "j";
int& j = (int&)i;
|
|
|
|
|
const int i = 10;
This statement declares to the compiler that every time it sees the name i in a line of source code, it should replace it with the value 10 . You have caused confusion in your source by declaring j as a reference to a non-const value of 10 . The two are not the same. You cannot cast away the const -ness of a value in this way and expect it to remain constant.
Binding 100,000 items to a list box can be just silly regardless of what pattern you are following. Jeremy Likness
|
|
|
|
|
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:
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
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.
|
|
|
|
|
Aabid wrote: the code declares a const integer and a reference to it. incrementing a reference adds 1 to j and outputs as 11, that is fine.
I don't see that it is "fine".
The code is wrong - it shouldn't be written like that in the first place.
Once one starts with that then the resulting behavior no longer matters.
There are an infinite number of ways to write code incorrectly. There is a large, but finite, number of ways to write code correctly. Myself I don't care what the compiler does with incorrect code.
|
|
|
|
|
|
are you talking about shellexecute win32 api verb?
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
yes,you can use win32 and api!
but it's best to use mfc
like:
you check a button,the dlg can show the "ppt" files
|
|
|
|
|
wangafei wrote: but it's best to use mfc
MFC has no such mechanism for what you are after. Use ShellExecute() as has been suggested.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"Show me a community that obeys the Ten Commandments and I'll show you a less crowded prison system." - Anonymous
|
|
|
|
|
|
HI,
I have an application in which is not using mfc.But i got a requirement that i need to create and display a dialog box. Existing application is a Regular DLL.
Thanks & Regards,
Rajeev
|
|
|
|
|
|
it's a good idea,but mfc si the best!
|
|
|
|
|
HI Alok,
Problem is existing application in non mfc. for displaying a small dialog do i need to make that existing application also Mfc one. please guide.
Thanks,
Rajeev
|
|
|
|
|
Rajeev.Goutham wrote: Problem is existing application in non mfc. for displaying a small dialog do i need to make that existing application also Mfc one. please guide.
No not necessary, you can create dialog box in non-mfc environment also. MFC is just wrapper around win32 based apis
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
yes! mfc can use api and win32
so you can do anything without mfc
but it's not easy
|
|
|
|
|
wangafei wrote: yes! mfc can use api and win32
so you can do anything without mfc
but it's not easy
Agreed, however it's the project choice to utilize services of MFC or not. if the project is targeted to basic win32, then let it be.. anybody who can program in MFC can program in win32 too.
"Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow Never mind - my own stupidity is the source of every "problem" - Mixture
cheers,
Alok Gupta
VC Forum Q&A :- I/ IV
Support CRY- Child Relief and You
|
|
|
|
|
Thanks Alok, will check that.
Thanks,
Rajeev
|
|
|
|
|
I am trying to draw image using bitblt method. When I try to
CBitmap bitmap;
bitmap.LoadBitmap(IDB_BITMAP1);
it works and image is getting drawn but when I use
Bitmap *bitmap = Bitmap::FromFile(L"d:\\Projects\\res\\state.bmp");
It does not draw any image...
Pleas help to sort out the problem. I need to draw image directly from path, not from resource and using bitblt only..
|
|
|
|