Click here to Skip to main content
16,005,473 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
GeneralRe: static variable Pin
George_George25-Aug-07 22:08
George_George25-Aug-07 22:08 
AnswerRe: static variable Pin
BadKarma23-Aug-07 22:10
BadKarma23-Aug-07 22:10 
GeneralRe: static variable Pin
George_George23-Aug-07 22:36
George_George23-Aug-07 22:36 
GeneralRe: static variable Pin
BadKarma23-Aug-07 23:24
BadKarma23-Aug-07 23:24 
GeneralRe: static variable Pin
George_George23-Aug-07 23:39
George_George23-Aug-07 23:39 
GeneralRe: static variable Pin
toxcct24-Aug-07 1:42
toxcct24-Aug-07 1:42 
GeneralRe: static variable Pin
George_George25-Aug-07 21:25
George_George25-Aug-07 21:25 
GeneralRe: static variable Pin
codeII24-Aug-07 1:49
codeII24-Aug-07 1:49 
int& MyFunction( )
{
static int localStatic = 0;
int nMyArray[ 10 ];
if ( localStatic < 10 )
{
//localStatic is not guaranteed < 10
nMyArray[ localStatic ] = 1;
}
return localStatic;
}

When one tread comes inside the “if statement” another tread could have set localStatic to for instance 11.

If you want to use a variable outside the scope of the function why are you not using a global?
When using a static in a function this “means” that the function is responsible for this variable. And it cannot, when this variable is managed outside this function. Keeping strict rules provides confusing situations, In terms of who is modifying what.

Another thing you should know is that when you are using a static within a class function, different instances of this class would use the same static instance; this can cause problems like the tread example above.

Using globals:
In header:
extern int g_MyVar;

In cpp
int g_MyVar = 0;

or when possible using a local variable giving to the function
void MyFunction( int& nValue )
{
nValue++;

}

int nValue = 0;
MyFunction( nValue );
GeneralRe: static variable Pin
George_George25-Aug-07 21:27
George_George25-Aug-07 21:27 
AnswerRe: static variable Pin
David Crow24-Aug-07 6:05
David Crow24-Aug-07 6:05 
GeneralRe: static variable Pin
George_George25-Aug-07 21:29
George_George25-Aug-07 21:29 
AnswerRe: static variable Pin
ghle26-Aug-07 3:42
ghle26-Aug-07 3:42 
GeneralRe: static variable Pin
George_George26-Aug-07 6:42
George_George26-Aug-07 6:42 
GeneralRe: static variable [modified] Pin
ghle26-Aug-07 10:00
ghle26-Aug-07 10:00 
GeneralRe: static variable Pin
George_George26-Aug-07 19:22
George_George26-Aug-07 19:22 
Questionregistry cleaner for window xp Pin
niki dutta23-Aug-07 18:26
niki dutta23-Aug-07 18:26 
AnswerRe: registry cleaner for window xp Pin
User 58385223-Aug-07 19:18
User 58385223-Aug-07 19:18 
AnswerRe: registry cleaner for window xp Pin
_AnsHUMAN_ 23-Aug-07 20:04
_AnsHUMAN_ 23-Aug-07 20:04 
AnswerRe: registry cleaner for window xp Pin
ThatsAlok23-Aug-07 20:11
ThatsAlok23-Aug-07 20:11 
AnswerPlease continue on the same thread !! Pin
toxcct23-Aug-07 21:57
toxcct23-Aug-07 21:57 
AnswerRe: registry cleaner for window xp Pin
Hamid_RT24-Aug-07 2:51
Hamid_RT24-Aug-07 2:51 
AnswerRe: registry cleaner for window xp Pin
David Crow24-Aug-07 6:18
David Crow24-Aug-07 6:18 
QuestionMFC Dialog I/O Pin
alberthyc23-Aug-07 17:53
alberthyc23-Aug-07 17:53 
AnswerRe: MFC Dialog I/O Pin
Nishad S23-Aug-07 21:21
Nishad S23-Aug-07 21:21 
AnswerRe: MFC Dialog I/O Pin
toxcct23-Aug-07 22:00
toxcct23-Aug-07 22:00 

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.