R
Russoue
I am having a strange problem. I have a static int with an initial
value in a header file which I include from two source files. In one
file, I assign a value to that int but the other file don't get it. I
have printed the value from both files. The first line printed from the
first file shows the assigned value but the second line printed from
the second file shows the initial value. How can this happen?
Here is an example:
File: Header.h
==============
#ifndef _HEADER_H_
#define _HEADER_H_
static int g_iVerboseLevel = 6;
#endif
File: Header2.h
===============
#ifndef _HEADER2_H_
#define _HEADER2_H_
void ChangeValue();
#endif
File: File2.cpp
===============
#include <iostream>
#include "Header.h"
using namespace std;
void ChangeValue()
{
std::cout << "Value from ChangeValue is " << g_iVerboseLevel <<
'\n';
}
File: File1.cpp
===============
#include "Header.h"
#include <iostream>
#include "Header2.h"
using namespace std;
int main(void)
{
g_iVerboseLevel = 4;
std::cout << "Value of g_iVerboseLevel from main is " <<
g_iVerboseLevel << '\n';
ChangeValue();
std::cout << "Value of g_iVerboseLevel from main is " <<
g_iVerboseLevel << endl;
return 0;
}
The output:
Value of g_iVerboseLevel from main is 4
Value from ChangeValue is 6
Value of g_iVerboseLevel from main is 4
Can anybody explain?
value in a header file which I include from two source files. In one
file, I assign a value to that int but the other file don't get it. I
have printed the value from both files. The first line printed from the
first file shows the assigned value but the second line printed from
the second file shows the initial value. How can this happen?
Here is an example:
File: Header.h
==============
#ifndef _HEADER_H_
#define _HEADER_H_
static int g_iVerboseLevel = 6;
#endif
File: Header2.h
===============
#ifndef _HEADER2_H_
#define _HEADER2_H_
void ChangeValue();
#endif
File: File2.cpp
===============
#include <iostream>
#include "Header.h"
using namespace std;
void ChangeValue()
{
std::cout << "Value from ChangeValue is " << g_iVerboseLevel <<
'\n';
}
File: File1.cpp
===============
#include "Header.h"
#include <iostream>
#include "Header2.h"
using namespace std;
int main(void)
{
g_iVerboseLevel = 4;
std::cout << "Value of g_iVerboseLevel from main is " <<
g_iVerboseLevel << '\n';
ChangeValue();
std::cout << "Value of g_iVerboseLevel from main is " <<
g_iVerboseLevel << endl;
return 0;
}
The output:
Value of g_iVerboseLevel from main is 4
Value from ChangeValue is 6
Value of g_iVerboseLevel from main is 4
Can anybody explain?