 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
ishekara Guest
|
Posted: Tue Sep 27, 2005 2:27 pm Post subject: static local variable in a member function. |
|
|
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;
}
|
|
| Back to top |
|
 |
Jaspreet Guest
|
Posted: Tue Sep 27, 2005 3:11 pm Post subject: Re: static local variable in a member function. |
|
|
ishekara wrote:
| Quote: | Hi,
Can you solve the puzzle for me?
|
Try reading about static keyword.
| Quote: |
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.
| Quote: | 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.
|
|
| Back to top |
|
 |
ishekara Guest
|
Posted: Tue Sep 27, 2005 3:24 pm Post subject: Re: static local variable in a member function. |
|
|
"Jaspreet" <jsingh.oberoi (AT) gmail (DOT) com> wrote
| Quote: | ishekara wrote:
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.
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.
| Quote: | 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
I tried to do the following change and was able to get different values |
namespace test1
{
| Quote: | #include "testinclude.h"
} |
using namespace test1;
| Quote: | int NewMethod1()
{
Include inc;
int i = inc.include1(1);
return i;
}
//test2.cpp
namespace test2 |
{
| Quote: | #include "testinclude.h"
} |
using namespace test2;
| Quote: | 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.
Thanks for help. |
|
|
| Back to top |
|
 |
Victor Bazarov Guest
|
Posted: Tue Sep 27, 2005 3:32 pm Post subject: Re: static local variable in a member function. |
|
|
ishekara wrote:
| Quote: | 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?
| Quote: | 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...
....and returned here.
| Quote: | }
};
//test1.cpp
#include "testinclude.h"
int NewMethod1()
|
This function is called first, right?
| Quote: | {
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'...
....which in turn gets returned here.
| Quote: | }
//test2.cpp
#include "testinclude.h"
int NewMethod2()
|
This function is called second, right?
| Quote: | {
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'.
And it's returned here.
V
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|