|
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..
|
|
|
|
|
I coded like this for my application.
CString bmpFilePath;
HBITMAP hBmp;
hBmp = (HBITMAP)::LoadImage(NULL,bmpFilePath,IMAGE_BITMAP,0,0,LR_LOADFROMFILE|LR_CREATEDIBSECTION);
CBitmap bmp;
bmp.Attach(hBmp);
CDC dcImage;
if(!dcImage.CreateCompatibleDC(pDC))
return;
BITMAP bm;
CBitmap* pOldBitmap;
bmp.GetBitmap(&bm);
pOldBitmap = dcImage.SelectObject(&bmp);
pDC->BitBlt(350,100, bm.bmWidth, bm.bmHeight, &dcImage, 0, 0, SRCCOPY);
dcImage.SelectObject(pOldBitmap);
Anu
|
|
|
|
|
|
Check bmp file is avaialble in folder.Otherwise it returns NULL.
Anu
|
|
|
|
|
Thanks its working...but what I need to change in code If I load a png/jpg image...
|
|
|
|
|
I use this code
HBITMAP mLoadImg(WCHAR *szFilename)
{
HBITMAP result=NULL;
Gdiplus::Bitmap* bitmap = new Gdiplus::Bitmap(szFilename,false);
bitmap->GetHBITMAP(NULL, &result);
delete bitmap;
return result;
}
Note: You have to startup and shutdown the GDI+ system. I usually do this in apps initialization and destruction (winmain or main if not mfc apps)
int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
Gdiplus::GdiplusStartupInput gdiplusStartupInput;
ULONG_PTR gdiplusToken;
Gdiplus::GdiplusStartup(&gdiplusToken, &gdiplusStartupInput, NULL);
hInst = hInstance;
InitCommonControls();
DialogBox(hInstance, MAKEINTRESOURCE(DLG_MAIN), NULL, (DLGPROC)DialogProc);
Gdiplus::GdiplusShutdown(gdiplusToken);
}
|
|
|
|
|
MFC by default support only BMP file, however you can take services of different library that suits your need, one of them is gdiplus, search Google for more information
"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
|
|
|
|
|
You've not shown the code that actually draws the image, but I'll take a guess at the problem. In your first code snippet, it is a CBitmap instance that will be used for drawing. In your second snippet, it is a pointer to a Bitmap instance that is used. Your drawing code will need to be adjusted accordingly.
Chris Meech
I am Canadian. [heard in a local bar]
In theory there is no difference between theory and practice. In practice there is. [Yogi Berra]
posting about Crystal Reports here is like discussing gay marriage on a catholic church’s website.[Nishant Sivakumar]
|
|
|
|