C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

static local variable in a member function.

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++)
View previous topic :: View next topic  
Author Message
ishekara
Guest





PostPosted: Tue Sep 27, 2005 2:27 pm    Post subject: static local variable in a member function. Reply with quote



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





PostPosted: Tue Sep 27, 2005 3:11 pm    Post subject: Re: static local variable in a member function. Reply with quote



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





PostPosted: Tue Sep 27, 2005 3:24 pm    Post subject: Re: static local variable in a member function. Reply with quote




"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





PostPosted: Tue Sep 27, 2005 3:32 pm    Post subject: Re: static local variable in a member function. Reply with quote

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...

Quote:
return i;

....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'...

Quote:
return 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'.

Quote:
return i;

And it's returned here.

Quote:
}

V

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ language (comp.lang.c++) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.