|
And there you have it.
So in short, you can use void if you want, but you should return an exit code of some sort.
Well, i suppose i should keep using int then
|
|
|
|
|
EDIT - after re-reading your post, my smart-alec response made no sense. I need more caffeine!
Cheers!
|
|
|
|
|
actually, it is the other way around.
void main() is illegal, you must always use int main(), but you don't *have* to return a value:
int main()
{
}
is valid c++
C on the other hand is a little different...
|
|
|
|
|
James Brown wrote: int main()
{
}
Hmm That wouldn't compile for me
|
|
|
|
|
James Brown wrote: because void main() is not part of the C++ standard
Standards schmandards! This is the Visual C++/MFC message board!
|
|
|
|
|
pity that MS share this philosophy though...
|
|
|
|
|
I've been asked to remove all access keys from an application's menu. (I know, stupid right?) Anyway, I've removed the access keys. However it turns out that when you press the ALT key and then the first letter of a submenu or menu item, it behaves just like an access key. Is this just inherent Windows behavior? Is there any way to prevent this behavior?
Thanks,
Stephanie
|
|
|
|
|
sdehart wrote: I've been asked to remove all access keys...
Do you mean the mnemonics? The "first letter" way of accessing a menu is built in.
"Approved Workmen Are Not Ashamed" - 2 Timothy 2:15
"Judge not by the eye but by the heart." - Native American Proverb
|
|
|
|
|
a well designed windows application is supposed to work even if no mouse is connected...
why on earth would someone break the mnemonics ?
|
|
|
|
|
This may be a dumb question, but I would like to know the answer.
1. A static variable is initialized only once, and remains in memory until the end of the program. It can only be accessed from the scope of the funtion. Correct?
2. A static function can only be accessed from the source file from within which it is contained. Correct?
3. A static class member is shared by all instances of the class, and only initialized once ( not counting initialization within the c'tor ). Also this type of function can be called from anywhere it is visible, ie not only from class instances. Correct?
4. But this one confuses the hell out of me. What about a static variable defined within a class function. I know a member variable is better suited for the job, but still... Am I correct in thinking it would be shared by all instances of the class?
|
|
|
|
|
WalderMort wrote: 1. A static variable is initialized only once, and remains in memory until the end of the program. It can only be accessed from the scope of the funtion. Correct?
Yes (I mean, for the last part it has to be declared in the function of course). But you can still change its value afterward (it's not a constant)
WalderMort wrote: 2. A static function can only be accessed from the source file from within which it is contained. Correct?
Don't know exactly what's the purpose of global static functions
WalderMort wrote: 3. A static class member is shared by all instances of the class, and only initialized once ( not counting initialization within the c'tor ).
Yes, a static member variable is shared among all instances but it is not initialised in the constructor but before the constructor gets called (at global scope in fact).
WalderMort wrote: Also this type of function can be called from anywhere it is visible, ie not only from class instances. Correct?
For static member functions, you don't need to have an instance to call it. You can simply call it this way: CMyClass::MyStaticFunc(); . This is true only if the function is public of course.
In fact the difference between a static and a non-static mem function is that the latter receive an implicit parameter: the this parameter (which identifies to which instance the function 'belongs to').
WalderMort wrote: 4. But this one confuses the hell out of me. What about a static variable defined within a class function. I know a member variable is better suited for the job, but still... Am I correct in thinking it would be shared by all instances of the class?
I would say that it is shared among all instances but I'm not 100% sure. In fact, I suppose it because for two different classes, it's the same function that will get called and it can know to which instance it belongs to because it received the implicit this parameter. So, I suppose that it works like a standard function. But for this point, I'm not totally sure but it is easy to test: just make two instances of a class with a function that increment the static variable and then prints the result and check the output.
|
|
|
|
|
4.
It's a class local variable (as opposed to an instance local variable)
|
|
|
|
|
Thanks for the explanations.
Cedric Moonen wrote: Don't know exactly what's the purpose of global static functions
I remember reading somewhere that this was a C thing, enabling two or more functions with the same name and paramaters to be defined, but each function is only avaialable to the single source file. Not entirely sure about it though.
For the 4th question. I honestly can't see the need for having a static variable in a function like this, EVER.
|
|
|
|
|
Yes - static functions at file scope are only visible within the source file they are declared and defined.
4th - if you have a function which needs to track something private to function but at class scope.
Any sufficiently gross incompetence is nearly indistinguishable from malice.
|
|
|
|
|
Blake Miller wrote: 4th - if you have a function which needs to track something private to function but at class scope.
This is what confuses me. Private to function, fine, but won't all the other instances of the same class have access to the same variable (memory location) when they themselves run the same function? If so, isn't it a little risky!
|
|
|
|
|
Not risky at all if that is what you intend.
No riskier than ANY static variable for a class, anyways
There are functions like InterlockedIncrement, critical sections, etc. to protect these variables, after all ... If by 'risk' you meant that you harbored some concerns about multi-threaded environments.
Any global or static variable whether at file, function, or class scope, left unprotected, by definition, is at risk in a multi-threaded environment.
Any sufficiently gross incompetence is nearly indistinguishable from malice.
|
|
|
|
|
Hi,
I've created a dialog application with only some static text modified so it should count the number of times a button is pressed. The text is the only thing in the dialog window.
I create an OnKeyDown-function to be used for keys being pressed. The problem is that nothing is shown in the dialog window when a key is pressed. I post my code to see if anyone can help me. What I want to happen is that the counter is counting as long as a specific key is pressed.
void CTry_6Dlg::OnKeyDown(UINT nChar, UINT nRepCnt, UINT nFlags) <br />
{<br />
if (nChar == VK_F3) {<br />
m_strCount.Format("%d", iCount);<br />
UpdateData(FALSE);<br />
iCount++;<br />
}<br />
<br />
CDialog::OnKeyDown(nChar, nRepCnt, nFlags);<br />
}
m_strCount is a CString connected to the static text and iCount is the counter that should be shown. I'm sure it is a simple question but since I'm a newbie to VC++ I can't figure it out.
Thanks for any help!
|
|
|
|
|
What is iCount ?
Did you try to step debug ? Are you really entering the function ?
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
|
|
|
|
|
iCount is a member variable of type int and it's initialized as iCount = 0 in the OnInitDialog initialization code.
I thought the function was entered when a WM_KEYDOWN message is sent?
|
|
|
|
|
I presume you put a break point in the method, ran debug and pressed a few keys and this did not work.
I would therefore suggest, that the message handler is not associated with the window that has focus. For example, (and I'm not saying your doing exactly this, but if you were to associate the message handler with a button on a dialog and the dialog had focus, the handler would not recieve any messages from any key clicks)
Tom
|
|
|
|
|
Ok, thanks for your reply.
What would be the best method to solve this problem?
Also, what I would like to do (in the final application) is to have combinations of buttons pressed. How can this be handled in a dialog app? What I did when I had it as a console app was that I used GetAsyncKeyState() but this doesn't seem to work in the dialog.
Thanks!
|
|
|
|
|
OK, the most important technique key to solving a problem like this is the use of Spy++. Start up your app and see what messages(WM_KEYDOWN) are going to what windows.
Once you know where the messages are going it's just a matter of attaching a handler to those windows.
In your case, I strongly suspect, most of the calls are going to the dialog box itself.
However, if they're not, there's no harm in putting the handlers all in the same file and in each one calling a master handler to get them all in the same place.
Regarding capturing key combinations, check out the nFlags parameter.
Mainly though it's a matter of check the VK_ codes and keeping count of when other keys are down. EG.
VK_CONTROL(WM_KEYDOWN) B(WM_KEYDOWN) B(WM_KEYUP) VK_CONTROL(WM_KEYUP) is control B.
Hope this is what your looking for
Tom
|
|
|
|
|
Thanks a lot. I'll check into it...
|
|
|
|
|
Hi there,
I have an application interfacing two other application one windows base and another one Linux base and my application receives messages from the Linux base app. and sends that to the windows base one and vise versa, the messages from Linux base app. in ASCII hex and I change it to XML string and send it to the windows base app. and also get in XML string from that and convert it to HEX for the Linux base app.
The problem is that after a while I get the Debug Assertion Failed dbgdel.cpp Line 42
Message and my application crashes.
Any idea would be appreciated.
Thanks
|
|
|
|
|
That's because you used ++ instead of + in line 21, making the counter in line 42 overflow.
Insert a comment line on line 41, this should fix it, or bring the problem to line 43.
~RaGE();
I think words like 'destiny' are a way of trying to find order where none exists. - Christian Graus
|
|
|
|