|
Some compilers initialize memory to zero. But for compatibility reasons, the C++ standard leaves (built-in type) members untouched (I don't know the latest one). Of course, if your members have default constructors, they will be called (before the constructor body) unless you place them in the initializer list.
|
|
|
|
|
w-peuker wrote: Some compilers initialize memory to zero. But for compatibility reasons, the C++ standard leaves (built-in type) members untouched (I don't know the latest one).
Wrong. The only compilers that leave data members non-initialized would be compilers that are not implementing the ANSI C++ specification.
Section 4.9.5 C++ specification clearly states that class member variables have a default value if they do not have an initializer.
class Any
{
....
int var1 = 2;
int var2;
}
<pre>
This happens regardless of how construction occurs.
|
|
|
|
|
See my answer above. You have misunderstood the standard. I have found many links that proove otherwise. By the way, your example is not even valid C++ code.
Philippe Mori
modified 27-Jul-12 21:04pm.
|
|
|
|
|
Philippe Mori wrote: See my answer above. You have misunderstood the standard.
Yes, you are correct.
|
|
|
|
|
It's just a matter of style, although I think the first option is preferred. MSDN has this to say[^] on the matter. You may also like to check if Bjarne Stroustrup has more to offer.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
|
Thanks George
Mark Brock
"We're definitely not going to make a G or a PG version of this. It's not PillowfightCraft." -- Chris Metzen
|
|
|
|
|
...and don't forget that the compiler processes the initializer list following the declaration order given in the class definition.
So you better stick to that order in the list (to prevent confusion). There are tools availabe that check this (e.g. cppcheck)
|
|
|
|
|
// Is there any particular reason
Yes :
class A
{
public:
A();
A(int);
A& operator=(int);
};
class B
{
A m_a;
public:
B() { m_a = 3; }
};
They sought it with thimbles, they sought it with care;
They pursued it with forks and hope;
They threatened its life with a railway-share;
They charmed it with smiles and soap.
|
|
|
|
|
The difference is that, in the first way of initialization (known as 'Initialization List'), the constructors of the initialized members are called.
While in the second option, all the objects get constructed before reaching the first executable statement of the constructor. Hence in the second way of initialization, the assignment operators gets called.
|
|
|
|
|
Dear all,
HINSTANCE handle = ShellExecute(NULL, NULL, strHlpFile, NULL, NULL, SW_SHOW);
I create a application in MFC, it works fine in Window XP and return 42 value, but if run it in Window 7, it cannot open the .CHM or PDF file, the return code is 8.
What's the problem? how can I solve issue?
I try to use system() to launch .CHM or PDF file, it works fine in both Window XP and Window 7, but it has MS-DOS black screen there, how can I let MS-DOS not shown?
Thank you very much!
|
|
|
|
|
Andraw111 wrote: What's the problem? Something memory related.
Andraw111 wrote: how can I solve issue? What is the value of strHlpFile ?
"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
|
|
|
|
|
Thanks for reply,
strHlpFile is the full path to .pdf file or .CHM file.
If it's about memory issue, how can I solve it?
|
|
|
|
|
I'm unsure of what file-path you're using, but on Win7 Home Premium this code works just fine:
#include <windows.h>
int main()
{
char *strPdfFile = "C:/Users/enhzflep/Documents/Books/programming/Web/html5-wtf.pdf";
char *strChmFile = "C:/Users/enhzflep/Documents/Books/programming/Web/php/php_enhanced_en.chm";
HINSTANCE handle = ShellExecute(NULL, NULL, strPdfFile, NULL, NULL, SW_SHOW);
handle = ShellExecute(NULL, NULL, strChmFile, NULL, NULL, SW_SHOW);
}
|
|
|
|
|
my code is exactly same as yours, but I still cannot make it run in Window 7, it works fine in Window XP.
My test mechine is Window 7 Enterprise. Is it possible related settings or security issue?
|
|
|
|
|
Not sure about what it could be.
As a test, I set my Acrobat to always run as admin. When I ran the code, I was just asked if I want to run Acrobat.exe or not.
When controlling COM objects - e.g MS Office. This behaviour is different.
I have office apps set to run as Admin, so I can write files anywhere I like. If I try to create (say) an 'Excel.Application' or 'Word.Application' object from php, C++, VB or javascript, it fails unless the calling application also has admin rights.
It sure sounds interesting. (but no-fun)
|
|
|
|
|
|
Find out and tell me, so I know too!
(ducks and runs for cover)
Nah, on a more serious note - did you try with lpOperation holding a pointer to the string "open"?
I've not seen the function used like this before, I've only ever seen it used with open specified.
HOWEVER - I just remembered David's comment about it being memory related earlier and thought to check-out the integer values for the error-codes. Looking into winerror.h we see the following:
#define ERROR_SUCCESS 0L
#define NO_ERROR 0L
#define ERROR_INVALID_FUNCTION 1L
#define ERROR_FILE_NOT_FOUND 2L
#define ERROR_PATH_NOT_FOUND 3L
#define ERROR_TOO_MANY_OPEN_FILES 4L
#define ERROR_ACCESS_DENIED 5L
#define ERROR_INVALID_HANDLE 6L
#define ERROR_ARENA_TRASHED 7L
#define ERROR_NOT_ENOUGH_MEMORY 8L
#define ERROR_INVALID_BLOCK 9L
#define ERROR_BAD_ENVIRONMENT 10L
#define ERROR_BAD_FORMAT 11L
#define ERROR_INVALID_ACCESS 12L
#define ERROR_INVALID_DATA 13L
#define ERROR_OUTOFMEMORY 14L
Which makes me feel foolish for not checking earlier, then going on to ask about the "open" verb. I guess your next bet would be to trawl the net for reports of it failing with an error of 8 (ERROR_NOT_ENOUGH_MEMORY)
Like I said - Interesting problem (but not a fun one)
Best,
Simon
|
|
|
|
|
Try passing a 0 instead of a NULL .
HINSTANCE handle = ShellExecute(0, NULL, strPdfFile, NULL, NULL, SW_SHOW);
|
|
|
|
|
Parameter 1 is an HWND so NULL is the correct value, and is equal to 0 so it would not have any effect. See here[^] for the full specification.
One of these days I'm going to think of a really clever signature.
|
|
|
|
|
True, Windows 7 UAC (User Account Control) screws things up.
http://en.wikipedia.org/wiki/User_Account_Control[^] says this about ShellExecute :
ShellExecute(hwnd, "runas", "C:\\Windows\\Notepad.exe", 0, 0, SW_SHOWNORMAL);
In the absence of a specific directive stating what privileges the application requests, UAC will apply heuristics, to determine whether or not the application needs administrator privileges. For example, if UAC detects that the application is a setup program, from clues such as the filename, versioning fields, or the presence of certain sequences of bytes within the executable, in the absence of a manifest it will assume that the application needs administrator privileges.
|
|
|
|
|
Now we have another small project also created in C++, it also launch .CHM file from help menu:
void CMainFrame::LaunchHelp(CString FilePath)
{
SHELLEXECUTEINFO sei;
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.fMask = NULL;
sei.hwnd = NULL;
sei.lpVerb = "open";
sei.lpFile = FilePath;
sei.lpParameters= NULL;
sei.nShow = SW_SHOWMAXIMIZED;
sei.hInstApp = NULL;
sei.lpIDList = NULL;
sei.lpClass = NULL;
sei.hkeyClass = NULL;
sei.dwHotKey = NULL;
sei.hIcon = NULL;
sei.hProcess = NULL;
sei.lpDirectory = NULL;
int ReturnCode = ::ShellExecuteEx(&sei);
switch(ReturnCode)
{
case ERROR_FILE_NOT_FOUND:
AfxMessageBox("The specified file was not found.");
break;
case ERROR_PATH_NOT_FOUND:
AfxMessageBox("The specified path was not found.");
break;
case ERROR_ACCESS_DENIED:
AfxMessageBox("Access to the specified file is denied.");
break;
}
}
I works fine in Window 7, I copy this function to my application (also created in C++), but after compile and test it in Window 7, still doesn't work, do we need change some settings to make app work in Window 7?
I compare the settings, the only difference are that
1: in C++ - >Code Generation -> Use run time library: I use Multithreaded DLL, they use Multlhreaded;
2: in General Microsoft Foundation Classes: I use Use MFC in a Shared Library; they use Use MFC in a Static Library.
Thanks!
|
|
|
|
|
The code below works and launches a helpfile in Windws 7.
#include <Windows.h>
#include <tchar.h>
#include <iostream.h>
int main()
{
SHELLEXECUTEINFO sei = {0};
sei.cbSize = sizeof(SHELLEXECUTEINFO);
sei.lpVerb = _T("open");
sei.lpFile = _T("C:\\windows\\help.CHM");
sei.fMask = SEE_MASK_INVOKEIDLIST;
sei.nShow = SW_SHOWDEFAULT;
DWORD exitCode;
int RetVal = GetExitCodeProcess(sei.hProcess, &exitCode);
cout<<"Errorcode "<<RetVal<<" \n";
if (ShellExecuteEx(&sei) == FALSE)
{
switch(RetVal)
{
case ERROR_FILE_NOT_FOUND:
break;
case ERROR_PATH_NOT_FOUND:
break;
case ERROR_ACCESS_DENIED:
break;
}
return FALSE;
}
return 0;
}
modified 25-Jul-12 13:42pm.
|
|
|
|
|
Now I cannot simply say that ::ShellExecuteEx(&sei) doesn't work in Window 7.
The problem is that it doesn't work in my application created before.
I have a app created using VC++ 6.0 last year, everything is fine except that I cannot open .CHM file from it.
Today I create a new application in MFC in VC++ 6.0, then use the exactly same function as the old application, it can open the .CHM file without any problem.
I check all the project settings for both application, they are same, why same function works in one application, but failed in another application?
|
|
|
|
|
I check the return code by:
int RetVal = GetExitCodeProcess(sei.hProcess, &exitCode);
both XP and window 7 return 0.
|
|
|
|