Thread: [Dev-C++] Static in member function
Open Source C & C++ IDE for Windows
Brought to you by:
claplace
|
From: Bjorn T. <bjo...@ho...> - 2003-09-16 19:13:42
|
If a static variable is declared inside a member function of a class, what
would happen?
That is, would all instances of the class use the same value, or would each
instance have its own copy of the variable?
The function is defined something like this:
int Array::unshift() // unshift
{
static int *active=first;
// the function is supposed to return the i:th element on the i:th call
}
Thanks for any help
Bjorn Terelius
_________________________________________________________________
Protect your PC - get McAfee.com VirusScan Online
http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
|
|
From: Per W. <pw...@ia...> - 2003-09-16 19:48:59
|
They would share the same variable.
/Per W
On Tue, 16 Sep 2003, Bjorn Terelius wrote:
> If a static variable is declared inside a member function of a class, what
> would happen?
> That is, would all instances of the class use the same value, or would each
> instance have its own copy of the variable?
>
>
> The function is defined something like this:
>
> int Array::unshift() // unshift
> {
> static int *active=first;
>
> // the function is supposed to return the i:th element on the i:th call
>
> }
>
>
> Thanks for any help
> Bjorn Terelius
>
> _________________________________________________________________
> Protect your PC - get McAfee.com VirusScan Online
> http://clinic.mcafee.com/clinic/ibuy/campaign.asp?cid=3963
>
>
>
> -------------------------------------------------------
> This sf.net email is sponsored by:ThinkGeek
> Welcome to geek heaven.
> http://thinkgeek.com/sf
> _______________________________________________
> Dev-cpp-users mailing list
> Dev...@li...
> TO UNSUBSCRIBE: http://www23.brinkster.com/noicys/devcpp/ub.htm
> https://lists.sourceforge.net/lists/listinfo/dev-cpp-users
>
|
|
From: <gen...@ya...> - 2003-09-20 02:59:10
|
Per Westmark's right, they would share the same variable, tho i'm not sure why... any answers would be appreciated... thanks NP --------------------------------- Want to chat instantly with your online friends? Get the FREE Yahoo!Messenger |
|
From: Per W. <pw...@ia...> - 2003-09-20 09:46:31
|
If I do
// Example 1
const char* myfunc(int i) {
static char my_buf[10];
sprintf(my_buf,"%d",i);
return my_buf;
}
the my_buf variable is actually a global variable but without a global
name. It is similar to
// Example 2
static char my_buf[10];
const char* myfunc(int i) {
sprintf(my_buf,"%d",i);
return my_buf;
}
with the exception that in the first case, noone in the same or other
source files may access my_buf, while in the second case everyone in the
same source file may access the my_buf[] variable, but the linker will not
export the variable name so code in other files will not be able to access
it.
If I then finally do:
// Example 3
char my_buf[10];
const char* myfunc(int i) {
sprintf(my_buf,"%d",i);
return my_buf;
}
then I'm all the way to a normal global variable, accessible by anyone.
In C++, there is one more alternative for the static keyword:
// Example 4
class MyClass {
protected:
static char my_buf[10];
public:
const char *myfunc(int i);
};
char MyClass::my_buf[10];
const char* MyClass::myfunc(int i) {
sprintf(my_buf,"%d",i);
return my_buf;
}
where everyone inherriting from MyClass may access MyClass::my_buf[]
since it is in the 'protected' section of the class definition, but where
all objects instanced from MyClass or a descendant of MyClass will share
the same variable. This is because MyClass::my_buf[] is a global variable
with a compile-time access restriction - in this case 'protected'.
In short, whenever the static keyword is used, there will be only one
instance of the variable, but depending on where the static keyword is
used, the access scope of the variable will be either a single file, a
sungle function or a single class.
/Per W
On Sat, 20 Sep 2003, Oladipo Odumosu wrote:
> Per Westmark's right, they would share the same variable, tho i'm not sur=
e why... any answers would be appreciated... thanks
>
> NP
>
>
>
>
> ---------------------------------
> Want to chat instantly with your online friends?=A0Get the FREE Yahoo!Mes=
senger
|
|
From: <gen...@ya...> - 2003-09-22 03:59:09
|
yo /Per,
4give me if i'm wrong, but i thought item 4 was for those variables which are *declared **in** the class* , not in functions belonging to the class. if my memory serves me right, the query was about
class test
{
public:
void f();
}
void test::f()
{
static int cow = 0;
cow++; std::cout << cow << std::endl;
}
or something likewise. if some1 will take Confucius away, i'll gladly get back to my TCPL :D.
---------------------------------
Want to chat instantly with your online friends? Get the FREE Yahoo!Messenger |
|
From: Per W. <pw...@ia...> - 2003-09-22 06:19:29
|
Declaring a static variable inside the implementation of a class method is
identical to declaring it inside a normal function, so - if I remember my
original numbering - it is my 2nd example.
I just wanted to display the different uses of static - for file-wide
variable, inside a function/method, and inside a class definition.
/Per W
On Mon, 22 Sep 2003, Oladipo Odumosu wrote:
>
> yo /Per,
>
> 4give me if i'm wrong, but i thought item 4 was for those variables which=
are *declared **in** the class* , not in functions belonging to the class.=
if my memory serves me right, the query was about
>
> class test
>
> {
>
> public:
>
> void f();
>
> }
>
>
>
> void test::f()
>
> {
>
> static int cow =3D 0;
>
> cow++; std::cout << cow << std::endl;
>
> }
>
> or something likewise. if some1 will take Confucius away, i'll gladly get=
back to my TCPL :D.
>
>
> ---------------------------------
> Want to chat instantly with your online friends?=A0Get the FREE Yahoo!Mes=
senger
|
|
From: <gen...@ya...> - 2003-09-20 02:51:55
|
Hello,
just in case no person has answered this (though i believe someone has), the answer is each object will have its own instance of the variable. in this case, the variable is simply a regular static variable in a function, and all the normal rules for static variable declared in a function apply.
cheers
NP
Bjorn Terelius <bjo...@ho...> wrote:
If a static variable is declared inside a member function of a class, what
would happen?
That is, would all instances of the class use the same value, or would each
instance have its own copy of the variable?
The function is defined something like this:
int Array::unshift() // unshift
{
static int *active=first;
// the function is supposed to return the i:th element on the i:th call
}
Thanks for any help
Bjorn Terelius
---------------------------------
Want to chat instantly with your online friends? Get the FREE Yahoo!Messenger |