static local variable in a member function.

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;
}
 
J

Jaspreet

ishekara said:
Hi,

Can you solve the puzzle for me?

Try reading about static keyword.
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.

There is always a single copy of the class. What the compiler generates
is multiple instances of the same class. So, you have mutiple objects
sharing the same code but having different instances of the data unless
the data is declared as static in which case they would all share the
same data member.
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;
}

Not sure why should you be expecting 2. i is a static variable which is
shared by all the instances of the class. It retains its value
in-between function calls. So, the first time you called include1(), i
got intialised to 1 and retained its value. The next call to include1()
still had i equal to 1, that is why 1 was returned and not 2. I am not
sure if you want i to be static. If you remove the static keyword, you
would get 2 as the return value.

Would advise you to read more on static.
 
I

ishekara

Jaspreet said:
Try reading about static keyword.


There is always a single copy of the class. What the compiler generates
is multiple instances of the same class. So, you have mutiple objects
sharing the same code but having different instances of the data unless
the data is declared as static in which case they would all share the
same data member.
I was thinking i should get different copies of the class. but i guess its
because
there are no different namespaces there is only one copy of the class in
global namespace.I tried to do the following change and was able to get different values
namespace test1
{}
using namespace test1;namespace test2
{}
using namespace test2;
Not sure why should you be expecting 2. i is a static variable which is
shared by all the instances of the class. It retains its value
in-between function calls. So, the first time you called include1(), i
got intialised to 1 and retained its value. The next call to include1()
still had i equal to 1, that is why 1 was returned and not 2. I am not
sure if you want i to be static. If you remove the static keyword, you
would get 2 as the return value.

Would advise you to read more on static.
Thanks for help.
 
V

Victor Bazarov

ishekara said:
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.

Why did you expect 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;

So, 'i' is only set to be equal to the argument value _iff_ its value is 0
at the time of call. Otherwise, its value is unchanged...
return i;

....and returned here.
}
};

//test1.cpp
#include "testinclude.h"
int NewMethod1()

This function is called first, right?
{
Include inc;
int i = inc.include1(1);

Calling 'include1' with argument '1' causes the static data object 'i'
inside that member function to be set to 1 and returned, initialising
this local automatic 'i'...
return i;

....which in turn gets returned here.
}

//test2.cpp
#include "testinclude.h"
int NewMethod2()

This function is called second, right?
{
Include inc;
int i = inc.include1(2 );

Calling 'include1' for a different object doesn't matter. The inner
variable 'i' of the 'Include::include1' member function is the same
as before, and it already has the value 1. So, the passed argument
'2' is ignored and the value of the static data object 'i' is given
back to initialise this function's local 'i'. The value of it is '1'.
return i;

And it's returned here.

V
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,056
Latest member
GlycogenSupporthealth

Latest Threads

Top