confused with "static"

A

Alexander Mahr

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
 
W

WW

Alexander said:
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.
 
A

Alexander Mahr

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
 
J

jeffc

Alexander Mahr said:
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.
 
J

jeffc

Alexander Mahr said:
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?
 
W

WW

Alexander said:
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.
 
A

Alexander Mahr

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
 
J

jeffc

Alexander Mahr said:
HI jeffc,


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

Alexander Mahr

:

[SNIP]
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
 
M

Mark Warren

Alexander Mahr said:
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;
 
W

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
 
A

Alexander Mahr

jeffc said:
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
 
J

jeffc

Alexander Mahr said:
:

[SNIP]
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;
 
R

Ron Natalie

Alexander Mahr said:
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).
 
R

Ron Natalie

Alexander Mahr said:
:

[SNIP]
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).
 
W

WW

Alexander said:
:

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

Alexander Mahr

jeffc said:
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
 
A

Alexander Mahr

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
 
A

Alexander Mahr

Ron said:
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
 
R

Ron Natalie

Alexander Mahr said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top