|
Thanks CPallini,
1.
So, in my original code, there are 3 instances,
- instance b and copied b to a new temp instance as the return value;
- instance s1 which is copy contructed from the temp instance.
Correct understanding?
2.
If I change the code in the following way, return the reference to local variable b, is it safe code?
#include <string>
using namespace std;
string& foo()
{
string b;
b.append ("msdn");
b.append (".microsoft");
b.append (".com");
return b;
}
int main()
{
string& s1 = foo();
return 0;
}
regards,
George
|
|
|
|
|
George_George wrote: 1.
So, in my original code, there are 3 instances,
- instance b and copied b to a new temp instance as the return value;
- instance s1 which is copy contructed from the temp instance.
Correct understanding?
Yes.
George_George wrote: 2.
If I change the code in the following way, return the reference to local variable b, is it safe code?
It's a mistake.
Change slightly the main function to see some output:
int main()
{
string& s1 = foo();
cout << s1 << endl;
return 0;
}
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks CPallini,
1.
For your comments, I think using reference is better since it will save instance creation and useless copy?
2.
I have tried to use cout, and the output is correct "msdn.microsoft.com", what is wrong?
regards,
George
|
|
|
|
|
George_George wrote: 1.
For your comments, I think using reference is better since it will save instance creation and useless copy?
As I have pointed out, using a reference that way is a mistake. You're getting a reference to a local object going out of scope (i.e. being destroyed) on function return.
George_George wrote: 2.
I have tried to use cout, and the output is correct "msdn.microsoft.com", what is wrong?
I see garbage on my system.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks CPallini,
Is it the best solution -- I mean saving the time to create new instance and safe code?
(In my sample below, there should be only one instance of string called b, correct?)
#include <string>
#include <iostream>
using namespace std;
void foo(string& b)
{
b.append ("msdn");
b.append (".microsoft");
b.append (".com");
return;
}
int main()
{
string b;
foo (b);
cout << b << endl;
return 0;
}
regards,
George
|
|
|
|
|
George_George wrote: Is it the best solution -- I mean saving the time to create new instance and safe code?
It's a good solution: correct code without create/destroy overhead.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong.
-- Iain Clarke
[My articles]
|
|
|
|
|
Thanks for code review, question answered.
regards,
George
|
|
|
|
|
See this MSDN forums thread[^]. In short, the Dinkumware C++ library (used by Microsoft) applies the "small string optimization" technique.
A more in depth article about std::string implementations can be found here[^]
|
|
|
|
|
|
I wanna hit some rows in the DataGrid,and the rows hit would be output to the excel.But I have not find the driver function that when I hit a row,what should I do?
|
|
|
|
|
How can I output the data to excel with VC?
|
|
|
|
|
Hi all,
I am doing an update system which gets updates of a software from a site. Now the update is in the form of a zip file. What I am using is:
ShellExecute( this->m_hWnd, "open", InstallerPath.c_str(), NULL, NULL, SW_SHOWNORMAL);
This is what I am using when it was direct to exe now the format has been changed to zip. Any advice on how I will do this. I want to be able to install the installer inside the zip file which is in .exe
Thanks,
Jayjay
|
|
|
|
|
Write code to
1. unzip the zip and
2. run the installer
3. Cleanup
Greetings from Germany
|
|
|
|
|
is there a function to unzip the zip?
|
|
|
|
|
Search CodeProject for Unzip.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
[Message Deleted]
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Sohail Kadiwala wrote: You could use Self-Extracting Zip Files ( For example see WinZip Self-Extractor[^])
So, you did not read the thread, did you?
Original Post: ... the format has been changed to zip ... I want to be able to install the installer inside the zip file which is in .exe
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
[Message Deleted]
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Sohail Kadiwala wrote: I did. I just mentioned that an alternate way would be to make it a self extracting installer. Winzip Self-Extractor was just an example.
Let me say it again: The OP is already receiving a zip file from somewhere, and his problem is to run the executable which is within that zip file. How can Winzip self-extractor help?
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
Winzip self-extractor was just an example. If you see the link I posted earlier, it gives usage details. He could have used his existing mechanism to launch the self-extractor instead of EXE
Rajesh R Subramanian wrote: s already receiving a zip file from somewhere
I believed the updates are controlled by him.
Sohail
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Sohail Kadiwala wrote: I believe the updates are controlled by him.
He is providing an update mechanism (which just executes a file using ShellExecute). The updates are coming to him from *a site*. Please read his original message. I won't be talking on this any further, as it serves no good to anybody.
Many are stubborn in pursuit of the path they have chosen, few in pursuit of the goal - Friedrich Nietzsche
.·´¯`·->Rajesh<-·´¯`·.
[Microsoft MVP - Visual C++]
|
|
|
|
|
I said "I believed" (past tense). I was not defending my answer but simply explaining what I believed and thought while answering.
I have deleted my previous posts. I agree, it serves no good to anybody and I'll stop here.
Sohail
modified 21-Apr-21 21:01pm.
|
|
|
|
|
Hi, i have written a small code to load a bitmap and display it on the window. The bitmap doesnt get displayed and i get only a blank window.I dont understand where the problem is.Please look at my code and do let me know if i am going wrong somewhere.Plse do help me out.Thnx in advance.
my code:
/*************************************************/
#include "windows.h"
#include "resource.h"
#include "iostream"
#include "tchar.h"
LRESULT CALLBACK WndProc(HWND,UINT,WPARAM,LPARAM);
typedef std::basic_string<TCHAR> ustring;
HBITMAP hBmp;
int WINAPI WinMain (HINSTANCE hThisInstance, HINSTANCE hPrevInstance,
LPSTR lpszArgument,int nFunsterStil)
{
HINSTANCE hInst = NULL;
ustring uClassName=_T("SIMPLEWND");
WNDCLASSEX wMyClass={0};
wMyClass.cbSize = sizeof(WNDCLASSEX);
wMyClass.lpfnWndProc = WndProc;
wMyClass.hInstance = hInst;
wMyClass.hIcon = (HICON)(LoadImage(0,IDI_APPLICATION, IMAGE_ICON,0,0,LR_SHARED));
wMyClass.hCursor = (HCURSOR)(LoadImage(0,IDC_ARROW,IMAGE_CURSOR,0,0,LR_SHARED));
wMyClass.hbrBackground = (HBRUSH)(COLOR_BTNFACE+1);
wMyClass.lpszClassName = uClassName.c_str();
if (!RegisterClassEx(&wMyClass))
{
MessageBox(NULL, "Failed to register wnd class", "Information", MB_OK);
return -1;
}
int desktopwidth=GetSystemMetrics(SM_CXSCREEN);
int desktopheight=GetSystemMetrics(SM_CYSCREEN);
HWND hwnd=CreateWindowEx(0,uClassName.c_str(),_T("Simple Window"),WS_OVERLAPPEDWINDOW,
desktopwidth/4,desktopheight/6,desktopwidth/2,desktopheight/2, 0, 0,
hInst, 0);
if (!hwnd)
{
MessageBox(NULL, "Failed to create wnd", "Information", MB_OK);
return -1;
}
ShowWindow(hwnd, 1);
UpdateWindow(hwnd);
hBmp =LoadBitmap(wMyClass.hInstance, MAKEINTRESOURCE(IDB_BITMAP1));
if( hBmp == NULL)
{
MessageBox(NULL, "Failed to load bmp", "Information", MB_OK);
UnregisterClass(uClassName.c_str(), hInst);
return -1;
}
UpdateWindow(hwnd);
MSG msg;
while (GetMessage(&msg,0,0,0)>0)
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
UnregisterClass(uClassName.c_str(), hInst);
}
int PaintFunc( HWND hwnd )
{
PAINTSTRUCT ps;
HDC hdc = BeginPaint(hwnd,&ps);
HDC hdcMem = CreateCompatibleDC(hdc);
HBITMAP hbmp =(HBITMAP) GetProp(hwnd,"hbmp");
if(hbmp) {
BITMAP bm;
GetObject(hbmp,sizeof(bm),(LPSTR)&bm);
hbmp = (HBITMAP)SelectObject(hdcMem,hbmp);
BitBlt(hdc,0,0,bm.bmWidth,bm.bmHeight,hdcMem,0,0,SRCCOPY);
hbmp = (HBITMAP)SelectObject(hdcMem,hbmp);
}
DeleteDC(hdcMem);
EndPaint(hwnd,&ps);
return 0;
}
LRESULT CALLBACK WndProc (HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam)
{
switch(uMsg)
{
case WM_DESTROY:
PostQuitMessage(0);
return 0;
case WM_PAINT:
PaintFunc(hWnd);
return 0;
default:
return DefWindowProc(hWnd, uMsg, wParam, lParam);
}
}
/*************************************************/
|
|
|
|
|
Try to replace LoadBitmap with LoadImage and use it with LR_CREATEDIBSECTION flag.
|
|
|
|
|
Using load image with LR_CREATEDIBSECTION is making no difference, the image is not getting displayed and i get the same blank window.
|
|
|
|
|