By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,813 Members | 1,253 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,813 IT Pros & Developers. It's quick & easy.

confused with "static"

P: n/a
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



Jul 19 '05 #1
Share this Question
Share on Google+
29 Replies


P: n/a
WW
Alexander Mahr wrote:
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)
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.
2. The data member is only accesable or "only exists" within the file
where the class is declared
Defined. You mix it up with global (namespace level) variable declarations.
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.?
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.
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?


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

Jul 19 '05 #2

P: n/a
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

Jul 19 '05 #3

P: n/a

"Alexander Mahr" <ma******@gmx.de> wrote in message
news:bk*************@news.t-online.com...
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
It also can mean that function variables persist across function calls.
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.?
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.
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?


Some people think so.
Jul 19 '05 #4

P: n/a

"Alexander Mahr" <ma******@gmx.de> wrote in message
news:bk*************@news.t-online.com...
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?
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.
Would this code compile and like without an error?


Why don't you try it and see?
Jul 19 '05 #5

P: n/a
WW
Alexander Mahr wrote:
Hi WW,

Since you know lot more like me about the keyword static
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] class CTest
{
public:
static int static_data_member;
[SNIP]
################# File 2: CTest.cpp

#include "CTest.h"

void CTest::memberfunction()
{
static_data_member=1; //Is this acces possible ?
Yes
I read static would limit acces to be just within
the file of where the static data member is declared?
Nope. not for member variables.
[SNIP] Would this code compile and like without an error?


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
Jul 19 '05 #6

P: n/a
HI jeffc,
Why don't you try it and see?


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
Jul 19 '05 #7

P: n/a

"Alexander Mahr" <ma******@gmx.de> wrote in message
news:bk*************@news.t-online.com...
HI jeffc,
Why don't you try it and see?
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.


I would suggest that it is *not* due to that reason or meaning of static.
Jul 19 '05 #8

P: n/a
"Alexander Mahr" <ma******@gmx.de> wrote in message
news:bk*************@news.t-online.com...
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?


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;
Jul 19 '05 #9

P: n/a
"WW" wrote:

[SNIP]
Would this code compile and like without an error?


Well, I did not read it all but if you mean if you can access the static
data member the answer is yes.


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

Jul 19 '05 #10

P: n/a
WW
[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
Jul 19 '05 #11

P: n/a

"Alexander Mahr" <ma******@gmx.de> wrote in message
news:bk*************@news.t-online.com...
"WW" wrote:

[SNIP]
Would this code compile and like without an error?
Well, I did not read it all but if you mean if you can access the static
data member the answer is yes.


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?


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;
Jul 19 '05 #12

P: n/a
"jeffc" wrote:

I would suggest that it is *not* due to that reason or meaning of static.


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

Jul 19 '05 #13

P: n/a

"Alexander Mahr" <ma******@gmx.de> wrote in message news:bk*************@news.t-online.com...
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?


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).
Jul 19 '05 #14

P: n/a

"Alexander Mahr" <ma******@gmx.de> wrote in message news:bk*************@news.t-online.com...
"WW" wrote:

[SNIP]
Would this code compile and like without an error?


Well, I did not read it all but if you mean if you can access the static
data member the answer is yes.


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

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).
Jul 19 '05 #15

P: n/a
WW
Alexander Mahr wrote:
"WW" wrote:

[SNIP]
Would this code compile and like without an error?


Well, I did not read it all but if you mean if you can access the
static data member the answer is yes.


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?


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
Jul 19 '05 #16

P: n/a
"jeffc" wrote:
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;


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
Jul 19 '05 #17

P: n/a
Thank you Ron
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).


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
Jul 19 '05 #18

P: n/a

"Alexander Mahr" <ma******@gmx.de> wrote in message news:bk*************@news.t-online.com...
BTW Do I have to define it in every file I want to acces it?

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.
Jul 19 '05 #19

P: n/a
WW
Alexander Mahr wrote:
Thank you Ron
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).


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?


No. Look at example #4 #5

--
WW aka Attila
Jul 19 '05 #20

P: n/a
Ron Natalie wrote:
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.

Thank you Ron,
I think now I got it. I really mixed it up like you describe it below
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).


Thank you

Cu ALexander
Jul 19 '05 #21

P: n/a

"WW" wrote
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.


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


Jul 19 '05 #22

P: n/a
"WW" wrote:
No. Look at example #4 #5


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

Jul 19 '05 #23

P: n/a
In article <bk*************@news.t-online.com>, ma******@gmx.de says...

[ ... ]
Does this mean you would suggest compiling the code below
shouldn't result in an error/ link error
I don't know what he would suggest, but your code is NOT correct, and an
error is to be expected with it.
########## File 1: CTest.h

class CTest
{
public:
static int static_data_member;
This declares, but does NOT define static_data_member.
CTest(){};
virtual ~CTest(){};
void memberfunction();
};

########## File 2: CTest.cpp

#include"CTest.h"

void CTest::memberfunction()
{
static_data_member=1;
}
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.

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


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.
Jul 19 '05 #24

P: n/a

"Alexander Mahr" <ma******@gmx.de> wrote in message
news:bk*************@news.t-online.com...
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.


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?)
Jul 19 '05 #25

P: n/a
WW
jeffc wrote:
[SNIP]
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?)


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
Jul 19 '05 #26

P: n/a

"WW" <wo***@freemail.hu> wrote in message
news:bk**********@phys-news1.kolumbus.fi...
jeffc wrote:
[SNIP]
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.


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.


That's what I just said. Nice of you to add another useless flame though.
Jul 19 '05 #27

P: n/a
WW
jeffc wrote:
"WW" <wo***@freemail.hu> wrote in message
news:bk**********@phys-news1.kolumbus.fi...
jeffc wrote:
[SNIP]
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.


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.


That's what I just said. Nice of you to add another useless flame
though.


Stop trolling.

--
WW aka Attila
Jul 19 '05 #28

P: n/a

"WW" <wo***@freemail.hu> wrote in message
news:bk**********@phys-news1.kolumbus.fi...

That's what I just said. Nice of you to add another useless flame
though.


Stop trolling.


Stop being pedantic.
Jul 19 '05 #29

P: n/a
WW
jeffc! Stop trolling.

--
WW aka Attila
Jul 19 '05 #30

This discussion thread is closed

Replies have been disabled for this discussion.