Click here to Skip to main content
15,904,416 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: why int main() ? Pin
Mark Salsbery10-Nov-06 5:17
Mark Salsbery10-Nov-06 5:17 
GeneralRe: why int main() ? Pin
Mark Salsbery10-Nov-06 5:21
Mark Salsbery10-Nov-06 5:21 
GeneralRe: why int main() ? Pin
James Brown10-Nov-06 5:27
James Brown10-Nov-06 5:27 
QuestionFirst letter of menu item is automatic access key? Pin
sdehart9-Nov-06 5:36
sdehart9-Nov-06 5:36 
AnswerRe: First letter of menu item is automatic access key? Pin
David Crow9-Nov-06 6:20
David Crow9-Nov-06 6:20 
AnswerRe: First letter of menu item is automatic access key? Pin
toxcct9-Nov-06 7:10
toxcct9-Nov-06 7:10 
QuestionStatic Pin
Waldermort9-Nov-06 4:28
Waldermort9-Nov-06 4:28 
AnswerRe: Static Pin
Cedric Moonen9-Nov-06 4:46
Cedric Moonen9-Nov-06 4:46 
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.






Cédric Moonen
Software developer

Charting control [Updated - v1.1]

GeneralRe: Static Pin
Niklas L9-Nov-06 4:56
Niklas L9-Nov-06 4:56 
GeneralRe: Static Pin
Waldermort9-Nov-06 5:27
Waldermort9-Nov-06 5:27 
GeneralRe: Static Pin
Blake Miller9-Nov-06 7:08
Blake Miller9-Nov-06 7:08 
GeneralRe: Static Pin
Waldermort9-Nov-06 8:32
Waldermort9-Nov-06 8:32 
GeneralRe: Static Pin
Blake Miller9-Nov-06 8:38
Blake Miller9-Nov-06 8:38 
QuestionOnKeyDown in dialog app... Pin
xkrja9-Nov-06 4:15
xkrja9-Nov-06 4:15 
AnswerRe: OnKeyDown in dialog app... Pin
Rage9-Nov-06 4:31
professionalRage9-Nov-06 4:31 
GeneralRe: OnKeyDown in dialog app... Pin
xkrja9-Nov-06 4:47
xkrja9-Nov-06 4:47 
GeneralRe: OnKeyDown in dialog app... Pin
TClarke9-Nov-06 5:04
TClarke9-Nov-06 5:04 
GeneralRe: OnKeyDown in dialog app... Pin
xkrja9-Nov-06 10:53
xkrja9-Nov-06 10:53 
GeneralRe: OnKeyDown in dialog app... Pin
TClarke9-Nov-06 23:35
TClarke9-Nov-06 23:35 
GeneralRe: OnKeyDown in dialog app... Pin
xkrja10-Nov-06 4:26
xkrja10-Nov-06 4:26 
QuestionDebug Assertion Failed dbgdel.cpp Line 45 Pin
nahitan9-Nov-06 4:13
nahitan9-Nov-06 4:13 
AnswerRe: Debug Assertion Failed dbgdel.cpp Line 45 Pin
Rage9-Nov-06 4:33
professionalRage9-Nov-06 4:33 
GeneralRe: Debug Assertion Failed dbgdel.cpp Line 45 Pin
Mark Salsbery9-Nov-06 5:11
Mark Salsbery9-Nov-06 5:11 
GeneralRe: Debug Assertion Failed dbgdel.cpp Line 45 Pin
nahitan9-Nov-06 6:06
nahitan9-Nov-06 6:06 
AnswerRe: Debug Assertion Failed dbgdel.cpp Line 45 Pin
Mark Salsbery9-Nov-06 6:14
Mark Salsbery9-Nov-06 6:14 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.