confused with "static" | | |
Dear Newsgroup,
I'm somehow confused with the usage of the static keyword.
I can see two function of the keyword static in conjunction with a data
member of a class.
1. The data member reffers in all objects of this class to the same data
Or in other word by using the static keyword all objects of one class
can share data. (This is what I want)
2. The data member is only accesable or "only exists" within the file where
the class is declared
The problem is now. That when I want to use the 1 function of the static
keyword I discribed here
but the member functions, which want to acces the static data member are
writen in another file (like its
usefull if you want to create a library) then there is the problem that in
this file the static data member
can't be accesed which results in a link error.?
Just a question wouldn't it have been better to create a keyword for the
first function "static" in the meaning
of once created in then static in memory and the second function which seems
like the oposide of the extern
keyword?
Thank you for your help
Alexander Mahr | | | | re: confused with "static"
Alexander Mahr wrote:[color=blue]
> Dear Newsgroup,
>
> I'm somehow confused with the usage of the static keyword.
>
> I can see two function of the keyword static in conjunction with a
> data member of a class.
>
> 1. The data member reffers in all objects of this class to the same
> data Or in other word by using the static keyword all objects of
> one class can share data. (This is what I want)[/color]
This is the one. Only one instance of that variable should exist (usually
in the implementation file for the class) and all class instances (objects)
access that one.
[color=blue]
> 2. The data member is only accesable or "only exists" within the file
> where the class is declared[/color]
Defined. You mix it up with global (namespace level) variable declarations.
[color=blue]
> The problem is now. That when I want to use the 1 function of the
> static keyword I discribed here
> but the member functions, which want to acces the static data member
> are writen in another file (like its
> usefull if you want to create a library) then there is the problem
> that in this file the static data member
> can't be accesed which results in a link error.?[/color]
Nope. If class A has a static member calles stat_member it can be accessed
as A::stat_member (if it is public or you are friend etc. - so it is
accessibler)from the "outside" and as stat_member from the inside (member
functions). All what the member functions need to see is the class
declaration. And that they need to see anyways.
[color=blue]
> Just a question wouldn't it have been better to create a keyword for
> the first function "static" in the meaning
> of once created in then static in memory and the second function
> which seems like the oposide of the extern
> keyword?[/color]
It would. The static keyword is severely "overloaded". There are those 4
meanings: static namespace level functions and variables (2), static member
variables and static local variables.
--
WW aka Attila | | | | re: confused with "static"
Hi WW,
Since you know lot more like me about the keyword static please
allow me to ask you with an example. First the example Code
################ File 1 : CTest.h
class CTest
{
public:
static int static_data_member;
CTest(){};
virtual ~CTest(){};
void memberfunction();
}
################# File 2: CTest.cpp
#include "CTest.h"
void CTest::memberfunction()
{
static_data_member=1; //Is this acces possible ? I read static would
limit
//acces to be just within the
file of where the static data member
//is declared?
}
############# end of example
Would this code compile and like without an error?
Since I get one with an similar code
Thank you WW,
CU Alexander | | | | re: confused with "static"
"Alexander Mahr" <mahralex@gmx.de> wrote in message
news:bksaav$hma$01$1@news.t-online.com...[color=blue]
> Dear Newsgroup,
>
> I'm somehow confused with the usage of the static keyword.
>
> I can see two function of the keyword static in conjunction with a data
> member of a class.
>
> 1. The data member reffers in all objects of this class to the same data
> Or in other word by using the static keyword all objects of one class
> can share data. (This is what I want)
>
> 2. The data member is only accesable or "only exists" within the file[/color]
where[color=blue]
> the class is declared[/color]
It also can mean that function variables persist across function calls.
[color=blue]
> The problem is now. That when I want to use the 1 function of the static
> keyword I discribed here
> but the member functions, which want to acces the static data member are
> writen in another file (like its
> usefull if you want to create a library) then there is the problem that in
> this file the static data member
> can't be accesed which results in a link error.?[/color]
No, when you use static for class member variables, it's a different usage
and doesn't imply file scope. You shouldn't have that problem.
[color=blue]
> Just a question wouldn't it have been better to create a keyword for the
> first function "static" in the meaning
> of once created in then static in memory and the second function which[/color]
seems[color=blue]
> like the oposide of the extern
> keyword?[/color]
Some people think so. | | | | re: confused with "static"
"Alexander Mahr" <mahralex@gmx.de> wrote in message
news:bksc83$qn4$06$1@news.t-online.com...[color=blue]
> Hi WW,
>
> Since you know lot more like me about the keyword static please
> allow me to ask you with an example. First the example Code
>
> ################ File 1 : CTest.h
>
> class CTest
> {
> public:
> static int static_data_member;
>
> CTest(){};
> virtual ~CTest(){};
> void memberfunction();
> }
>
>
> ################# File 2: CTest.cpp
>
> #include "CTest.h"
>
> void CTest::memberfunction()
> {
> static_data_member=1; //Is this acces possible ? I read static[/color]
would[color=blue]
> limit
> //acces to be just within the
> file of where the static data member
> //is declared?[/color]
As he said, that is a different meaning of static that doesn't apply here.
However, that's still not the way you would do it.
[color=blue]
> Would this code compile and like without an error?[/color]
Why don't you try it and see? | | | | re: confused with "static"
Alexander Mahr wrote:[color=blue]
> Hi WW,
>
> Since you know lot more like me about the keyword static[/color]
Ah. Me flattered. :-) I program in C for along and it was already there
"overloaded" 3 ways. And in school one has time to learn. :-)
[SNIP][color=blue]
> class CTest
> {
> public:
> static int static_data_member;[/color]
[SNIP][color=blue]
>
> ################# File 2: CTest.cpp
>
> #include "CTest.h"
>
> void CTest::memberfunction()
> {
> static_data_member=1; //Is this acces possible ?[/color]
Yes
[color=blue]
> I read static would limit acces to be just within
> the file of where the static data member is declared?[/color]
Nope. not for member variables.
[SNIP][color=blue]
> Would this code compile and like without an error?[/color]
Well, I did not read it all but if you mean if you can access the static
data member the answer is yes.
--
WW aka Attila | | | | re: confused with "static"
HI jeffc,
[color=blue]
> Why don't you try it and see?[/color]
First of all , thank you for your reply
I have tried it already but I had to use a smaller class for my example so I
shortened
my code and I had not tested as it had stood in my posting the time I posted
it.
but now I have and My Compiler/Linker tells me : "error LNK2001"
Which in my idea is a link error and is caused by the so cold within a file
limitation or the so called
file scope of static declared data members.
Or is my linker/compiler defective?
I use Ms visual C++ 6 prof.
Cu Alexander | | | | re: confused with "static"
"Alexander Mahr" <mahralex@gmx.de> wrote in message
news:bkse2q$sk6$06$1@news.t-online.com...[color=blue]
> HI jeffc,
>[color=green]
> > Why don't you try it and see?[/color]
>
> First of all , thank you for your reply
> I have tried it already but I had to use a smaller class for my example so[/color]
I[color=blue]
> shortened
> my code and I had not tested as it had stood in my posting the time I[/color]
posted[color=blue]
> it.
>
> but now I have and My Compiler/Linker tells me : "error LNK2001"
> Which in my idea is a link error and is caused by the so cold within a[/color]
file[color=blue]
> limitation or the so called
> file scope of static declared data members.[/color]
I would suggest that it is *not* due to that reason or meaning of static. | | | | re: confused with "static"
"Alexander Mahr" <mahralex@gmx.de> wrote in message
news:bksc83$qn4$06$1@news.t-online.com...[color=blue]
> Hi WW,
>
> Since you know lot more like me about the keyword static please
> allow me to ask you with an example. First the example Code
>
> ################ File 1 : CTest.h
>
> class CTest
> {
> public:
> static int static_data_member;
>
> CTest(){};
> virtual ~CTest(){};
> void memberfunction();
> }
>
>
> ################# File 2: CTest.cpp
>
> #include "CTest.h"
>
> void CTest::memberfunction()
> {
> static_data_member=1; //Is this acces possible ? I read static[/color]
would[color=blue]
> limit
> //acces to be just within the
> file of where the static data member
> //is declared?
> }
>
> ############# end of example
>
> Would this code compile and like without an error?[/color]
No. You need to declare space for the static data member. CTest.cpp needs
a line like
int CTest::static_data_member;
or
int CTest::static_data_member = 1; | | | | re: confused with "static"
"WW" wrote:
[SNIP][color=blue][color=green]
> > Would this code compile and like without an error?[/color]
>
> Well, I did not read it all but if you mean if you can access the static
> data member the answer is yes.[/color]
Do you mean "Yes one can acces it from within a different file as too"
Well I really can acces it but after compiling without error the liner tells
me
error LNK2001 : "unkown extern sysmbol" static_data_member
Any idea?
Is my linker not working correctly?
thank you for your help ww
Cu Alexander | | | | re: confused with "static"
[HUGE SNIP]
OK. Recap.
In #1 and # all declarations of bamboo are "global". (Not in a function or
a class-type.)
#1-#3 is deliberately written as C, since it does exist in C.
WARNING: all code untested
=====================
Static #1:
// file1.cpp
static int bamboo; // Has internal lingake
// That means its name is not visible to
// "anyone" else but those who are "after"
// it in file1.cpp, so:
// file1b.cpp with no bamboo anywhere declared:
int x=bamboo; // error
// file1c.cpp
chart *bamboo; // OK, no other bamboo
=====================
Static #2:
// file2.cpp
static int bamboo(char *); // Has internal lingake
// That means its name is not visible to
// "anyone" else but those who are "after"
// it in file1.cpp, so:
// file2b.cpp with no bamboo anywhere declared:
int x=bamboo("Chineese"); // error
// file2c.cpp
chart *bamboo; // OK, no other bamboo
=====================
Static #3:
#include <stdio.h>
int counter( int *p) {
static int i = -1;
if (p) i = p; else ++i;
return i;
}
int main() {
printf( "%d\n", counter(NULL));
printf( "%d\n", counter(NULL));
printf( "%d\n", counter(12));
printf( "%d\n", counter(NULL));
printf( "%d\n", counter(0));
}
Output will be:
0
1
12
13
0
=====================
Static #4:
// ctr.h
struct Counted {
Counted();
Counted(Counted const &);
~Counted();
static int count;
};
// This class is broken,
// since count is public.
// But it is needed for
// all the examples
// file4.cpp
int Counted::count = 0;
// file4a.cpp
Counted::Counted() {
++count;
}
Counted::Counted(Counted const &) {
++count;
}
// and so on
// file4b.cpp
std::cout << Counter::count;
// Possible, count is public.
// No need for a Counter object
=====================
Static #5:
// ctr.h
// We will hide the count!
struct Counted {
Counted();
Counted(Counted const &);
~Counted();
static int getCount();
// static int getCount() const;
// would be an error, since there is
// no object for a static member
// function, it can only see
// the static member variables.
private:
static int count;
int somethingelse;
};
// file5.cpp
int Counted::count = 0;
// file5a.cpp
Counted::Counted() {
++count;
}
Counted::Counted(Counted const &o) {
++count;
somethingelse = o.somethingelse;
}
Counted::getCount() {
// somethingelse = 10; would be
// an error. There is no hidden
// this pointer argument in a static
// function so non-static data members
// cannot be used: there is no object;
// static member function "work on the
// class, they only see the static
// data members
return count;
}
// and so on
// file5b.cpp
std::cout << Counter::getCount();
// No need for a Counter object
--
WW aka Attila | | | | re: confused with "static"
"Alexander Mahr" <mahralex@gmx.de> wrote in message
news:bkseem$lmg$01$1@news.t-online.com...[color=blue]
> "WW" wrote:
>
> [SNIP][color=green][color=darkred]
> > > Would this code compile and like without an error?[/color]
> >
> > Well, I did not read it all but if you mean if you can access the static
> > data member the answer is yes.[/color]
>
> Do you mean "Yes one can acces it from within a different file as too"
> Well I really can acces it but after compiling without error the liner[/color]
tells[color=blue]
> me
>
> error LNK2001 : "unkown extern sysmbol" static_data_member
>
> Any idea?[/color]
You are just not using the correct technique for static members. You
*declared* it in your class. But this is not *defining* it. It is not
creating storage for it. Somewhere in your source file you have to *define*
it with
int CTest::static_data_member; | | | | re: confused with "static"
"jeffc" wrote:[color=blue]
>
> I would suggest that it is *not* due to that reason or meaning of static.
>[/color]
Does this mean you would suggest compiling the code below
shouldn't result in an error/ link error
########## File 1: CTest.h
class CTest
{
public:
static int static_data_member;
CTest(){};
virtual ~CTest(){};
void memberfunction();
};
########## File 2: CTest.cpp
#include"CTest.h"
void CTest::memberfunction()
{
static_data_member=1;
}
int main()
{
CTest ctest;
//do nothing
return 0;
}
Thank you for your help,
BTw if you have to much time copy the code and save it into the files
CTest.h and
CTest.cpp and try compiling and linking CTest.cpp I would like to know if
there is
only my copmiler / linker is defective
Thank you jeffc
CU Alexander
CTest | | | | re: confused with "static"
"Alexander Mahr" <mahralex@gmx.de> wrote in message news:bksc83$qn4$06$1@news.t-online.com...
[color=blue]
> static_data_member=1; //Is this acces possible ? I read static would
> limit
> //acces to be just within the
> file of where the static data member
> //is declared?[/color]
It's legal. You're confusing the multiple uses of the static keyword.
The static keyword when applied to a declaration outside a class or
a function means to force internal linkage (can't get to it outside
the file). That doesn't apply to static class members.
Even if you were talking about static global variables, it's not
the "file" that matters but the "translation unit." This is the
initial CPP file you give plus all the things that get #included
into it. Since you include CTest.h into CTest.cpp any statics
that you declare in the .h file will be accessible to the rest of
the TU (including alll of the .cpp after that inclusion). | | | | re: confused with "static"
"Alexander Mahr" <mahralex@gmx.de> wrote in message news:bkseem$lmg$01$1@news.t-online.com...[color=blue]
> "WW" wrote:
>
> [SNIP][color=green][color=darkred]
> > > Would this code compile and like without an error?[/color]
> >
> > Well, I did not read it all but if you mean if you can access the static
> > data member the answer is yes.[/color]
>
> Do you mean "Yes one can acces it from within a different file as too"
> Well I really can acces it but after compiling without error the liner tells
> me
>
> error LNK2001 : "unkown extern sysmbol" static_data_member
>[/color]
Different reason. You must DEFINE static members in addition
to declaring it. In your CPP file you need to do
int CTest::static_data_member;
outside of any function/class definition.
You can even provide an initializer if you want (otherwise it will be default
initialized). | | | | re: confused with "static"
Alexander Mahr wrote:[color=blue]
> "WW" wrote:
>
> [SNIP][color=green][color=darkred]
>>> Would this code compile and like without an error?[/color]
>>
>> Well, I did not read it all but if you mean if you can access the
>> static data member the answer is yes.[/color]
>
> Do you mean "Yes one can acces it from within a different file as too"
> Well I really can acces it but after compiling without error the
> liner tells me
>
> error LNK2001 : "unkown extern sysmbol" static_data_member
>
>
> Any idea?
>
> Is my linker not working correctly?[/color]
Nope. You did not define the static data member. I told you I was only
looking at your code from the point of view of the access of the member.
--
WW aka Attila | | | | re: confused with "static"
"jeffc" wrote:[color=blue]
> You are just not using the correct technique for static members. You
> *declared* it in your class. But this is not *defining* it. It is not
> creating storage for it. Somewhere in your source file you have to[/color]
*define*[color=blue]
> it with
> int CTest::static_data_member;
>[/color]
Hi Jeffc,
Now I know what I got wrong all the time. As you said I forgot to define the
static member,
because I thought I would have done this already in the class decleration.
When then the error
appeared I browsed MSDN and google for an explenation and I found something
which tells
static (which is really a bit overloaded) causes variables to be "not
extern" and since I used
two files and accesd the static data member from the other file I thought
this was the reason
for the lnk error.
But now I know: I have to define first.
Thank you for your help
Thanks to WW to who also explained a lot
Tank you !!!!
Cu Alexander | | | | re: confused with "static"
Thank you Ron[color=blue]
> Different reason. You must DEFINE static members in addition
> to declaring it. In your CPP file you need to do
> int CTest::static_data_member;
> outside of any function/class definition.
>
> You can even provide an initializer if you want (otherwise it will be[/color]
default[color=blue]
> initialized).[/color]
I got it now, I just thought all the time the error was caused by another
behaivior of
static where it is used as an "anti-extern".
BTW Do I have to define it in every file I want to acces it?
Thank you Alexander | | | | re: confused with "static"
"Alexander Mahr" <mahralex@gmx.de> wrote in message news:bksg8d$a82$02$1@news.t-online.com...
[color=blue]
> BTW Do I have to define it in every file I want to acces it?
>[/color]
No, you specifically do not want to that. It needs to get defined
(storage allocated and initialized) only in one place. If you do
it more than once, then you will get complaints about having
too many at link time. | | | | re: confused with "static"
Alexander Mahr wrote:[color=blue]
> Thank you Ron[color=green]
>> Different reason. You must DEFINE static members in addition
>> to declaring it. In your CPP file you need to do
>> int CTest::static_data_member;
>> outside of any function/class definition.
>>
>> You can even provide an initializer if you want (otherwise it will
>> be default initialized).[/color]
>
> I got it now, I just thought all the time the error was caused by
> another behaivior of
> static where it is used as an "anti-extern".
> BTW Do I have to define it in every file I want to acces it?[/color]
No. Look at example #4 #5
--
WW aka Attila | | | | re: confused with "static"
Ron Natalie wrote:[color=blue]
> It's legal. You're confusing the multiple uses of the static keyword.
> The static keyword when applied to a declaration outside a class or
> a function means to force internal linkage (can't get to it outside
> the file). That doesn't apply to static class members.
>[/color]
Thank you Ron,
I think now I got it. I really mixed it up like you describe it below
[color=blue]
> Even if you were talking about static global variables, it's not
> the "file" that matters but the "translation unit." This is the
> initial CPP file you give plus all the things that get #included
> into it. Since you include CTest.h into CTest.cpp any statics
> that you declare in the .h file will be accessible to the rest of
> the TU (including alll of the .cpp after that inclusion).
>
>[/color]
Thank you
Cu ALexander | | | | re: confused with "static"
"WW" wrote
[color=blue]
> Nope. You did not define the static data member. I told you I was only
> looking at your code from the point of view of the access of the member.[/color]
Thank you very much WW. Now I know what you meant but I didn't got it at
once
because I'm not very expiered in programming.
Thank you for your help
Cu Alexander | | | | re: confused with "static"
"WW" wrote:[color=blue]
> No. Look at example #4 #5
>[/color]
I will do this at once and save these really helpfull examples at once.
Thank you once again for your Help and your time.
Cu Alexander | | | | re: confused with "static"
In article <bksf4o$7lj$05$1@news.t-online.com>, mahralex@gmx.de says...
[ ... ]
[color=blue]
> Does this mean you would suggest compiling the code below
> shouldn't result in an error/ link error[/color]
I don't know what he would suggest, but your code is NOT correct, and an
error is to be expected with it.
[color=blue]
> ########## File 1: CTest.h
>
> class CTest
> {
> public:
> static int static_data_member;[/color]
This declares, but does NOT define static_data_member.
[color=blue]
> CTest(){};
> virtual ~CTest(){};
> void memberfunction();
> };
>
> ########## File 2: CTest.cpp
>
> #include"CTest.h"
>
> void CTest::memberfunction()
> {
> static_data_member=1;
> }[/color]
Somewhere here (i.e. at namespace scope) you need to add a definition of
the variable:
int CTest::static_data_member;
Now the variable is defined, and the error you were getting should be
eliminated.
[ ... ]
[color=blue]
> BTw if you have to much time copy the code and save it into the files
> CTest.h and CTest.cpp and try compiling and linking CTest.cpp I would
> like to know if there is only my copmiler / linker is defective[/color]
VC++ 6 is far from perfect, but the problem you're encountering is in
your own code, not the compiler or linker.
--
Later,
Jerry.
The universe is a figment of its own imagination. | | | | re: confused with "static"
"Alexander Mahr" <mahralex@gmx.de> wrote in message
news:bksg1c$ne6$01$1@news.t-online.com...[color=blue]
> Now I know what I got wrong all the time. As you said I forgot to define[/color]
the[color=blue]
> static member,
> because I thought I would have done this already in the class decleration.
> When then the error
> appeared I browsed MSDN and google for an explenation and I found[/color]
something[color=blue]
> which tells
> static (which is really a bit overloaded) causes variables to be "not
> extern" and since I used
> two files and accesd the static data member from the other file I thought
> this was the reason
> for the lnk error.[/color]
This is just my opinion, but this is one of the weaknesses of C++. In an
effort to avoid adding new keywords which might make some C programs be not
compatible, in the long run they probably wasted far more time with all the
confusion these sorts of things have caused programmers, such as yourself.
Yes, it's called "code", but that doesn't mean it really has to be
enigmatic. (This isn't a perfect example, because the confusing meaning of
static already existed in C. For a better example, would the world really
have stopped spinning if they added a purevirtual keyword? Or abstractclass
for that matter?) | | | | re: confused with "static"
jeffc wrote:
[SNIP][color=blue]
> This is just my opinion, but this is one of the weaknesses of C++.
> In an effort to avoid adding new keywords which might make some C
> programs be not compatible, in the long run they probably wasted far
> more time with all the confusion these sorts of things have caused
> programmers, such as yourself. Yes, it's called "code", but that
> doesn't mean it really has to be enigmatic. (This isn't a perfect
> example, because the confusing meaning of static already existed in
> C. For a better example, would the world really have stopped
> spinning if they added a purevirtual keyword? Or abstractclass for
> that matter?)[/color]
static was already 3 times overloaded in C. I do not see severe added
complexity in C++ and the C++ use of static aligns with the C one. For me
it took 5 minutes to get it, but I was using C for longer time at that time.
--
WW aka Attila | | | | re: confused with "static"
"WW" <wolof@freemail.hu> wrote in message
news:bksn87$4hq$1@phys-news1.kolumbus.fi...[color=blue]
> jeffc wrote:
> [SNIP][color=green]
> > This is just my opinion, but this is one of the weaknesses of C++.
> > In an effort to avoid adding new keywords which might make some C
> > programs be not compatible, in the long run they probably wasted far
> > more time with all the confusion these sorts of things have caused
> > programmers, such as yourself. Yes, it's called "code", but that
> > doesn't mean it really has to be enigmatic. (This isn't a perfect
> > example, because the confusing meaning of static already existed in
> > C.[/color]
>
> static was already 3 times overloaded in C. I do not see severe added
> complexity in C++ and the C++ use of static aligns with the C one.[/color]
That's what I just said. Nice of you to add another useless flame though. | | | | re: confused with "static"
jeffc wrote:[color=blue]
> "WW" <wolof@freemail.hu> wrote in message
> news:bksn87$4hq$1@phys-news1.kolumbus.fi...[color=green]
>> jeffc wrote:
>> [SNIP][color=darkred]
>>> This is just my opinion, but this is one of the weaknesses of C++.
>>> In an effort to avoid adding new keywords which might make some C
>>> programs be not compatible, in the long run they probably wasted far
>>> more time with all the confusion these sorts of things have caused
>>> programmers, such as yourself. Yes, it's called "code", but that
>>> doesn't mean it really has to be enigmatic. (This isn't a perfect
>>> example, because the confusing meaning of static already existed in
>>> C.[/color]
>>
>> static was already 3 times overloaded in C. I do not see severe
>> added complexity in C++ and the C++ use of static aligns with the C
>> one.[/color]
>
> That's what I just said. Nice of you to add another useless flame
> though.[/color]
Stop trolling.
--
WW aka Attila | | | | re: confused with "static"
"WW" <wolof@freemail.hu> wrote in message
news:bktokn$5do$1@phys-news1.kolumbus.fi...[color=blue][color=green]
> >
> > That's what I just said. Nice of you to add another useless flame
> > though.[/color]
>
> Stop trolling.[/color]
Stop being pedantic. | | | | re: confused with "static"
jeffc! Stop trolling.
--
WW aka Attila |  | | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,384 network members.
|