I
ishekara
Hi,
Can you solve the puzzle for me?
I have a main.cpp, testinclude.h, and test1.cpp and test2.cpp which include
testinclude.h
testinclude.h has a class Include defined. I always thought that the
compiler generates a copy of
class Include one each for test1.cpp and test2.cpp. But however they use the
same class definition for both test1.cpp and test2.cpp.
Following is the code.
//main.cpp
int NewMethod1();
int NewMethod2();
int main()
{
int i=NewMethod1();
i=NewMethod2(); // this function returns 1 where in i was expecting it to
return 2.
return i;
}
//testinclude.h
class Include
{
public:
Include(){}
int include1(int iN)
{
static int i = 0;
if(i == 0)
i = iN;
return i;
}
};
//test1.cpp
#include "testinclude.h"
int NewMethod1()
{
Include inc;
int i = inc.include1(1);
return i;
}
//test2.cpp
#include "testinclude.h"
int NewMethod2()
{
Include inc;
int i = inc.include1(2 );
return i;
}
Can you solve the puzzle for me?
I have a main.cpp, testinclude.h, and test1.cpp and test2.cpp which include
testinclude.h
testinclude.h has a class Include defined. I always thought that the
compiler generates a copy of
class Include one each for test1.cpp and test2.cpp. But however they use the
same class definition for both test1.cpp and test2.cpp.
Following is the code.
//main.cpp
int NewMethod1();
int NewMethod2();
int main()
{
int i=NewMethod1();
i=NewMethod2(); // this function returns 1 where in i was expecting it to
return 2.
return i;
}
//testinclude.h
class Include
{
public:
Include(){}
int include1(int iN)
{
static int i = 0;
if(i == 0)
i = iN;
return i;
}
};
//test1.cpp
#include "testinclude.h"
int NewMethod1()
{
Include inc;
int i = inc.include1(1);
return i;
}
//test2.cpp
#include "testinclude.h"
int NewMethod2()
{
Include inc;
int i = inc.include1(2 );
return i;
}