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 29 4310
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
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
"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.
"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?
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
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
"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.
"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;
"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
[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
"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;
"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
"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).
"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).
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
"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
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
"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.
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
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
"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
"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
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.
"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?)
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
"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.
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
"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.
jeffc! Stop trolling.
--
WW aka Attila This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: comp.lang.php |
last post by:
function blah($item) {
if (!isset($baseDir)) {
static $baseDir = '';
$baseDir = $item;
print_r("baseDir = $baseDir\n");
}
$dirID = opendir($item);
while (($fyl = readdir($dirID)) !== false)...
|
by: Joseph Turian |
last post by:
Consider this code snippet which doesn't compile:
struct DebugOptions {
};
class Debug {
public:
Debug(const DebugOptions options) { _options = options; }
private:
|
by: Jon E. Scott |
last post by:
I'm a little confused with "static" methods and how to access other unstatic
methods. I'm a little new to C#.
I'm testing a callback routine within a DLL and the callback function
returns a...
|
by: Jess |
last post by:
Hello,
I learned that there are five kinds of static objects, namely
1. global objects
2. object defined in namespace scope
3. object declared static instead classes
4. objects declared...
|
by: lllomh |
last post by:
Define the method first
this.state = {
buttonBackgroundColor: 'green',
isBlinking: false, // A new status is added to identify whether the button is blinking or not
}
autoStart=()=>{
|
by: DJRhino |
last post by:
Was curious if anyone else was having this same issue or not....
I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 4 Oct 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM)
The start time is equivalent to 19:00 (7PM) in Central...
|
by: Aliciasmith |
last post by:
In an age dominated by smartphones, having a mobile app for your business is no longer an option; it's a necessity. Whether you're a startup or an established enterprise, finding the right mobile app...
|
by: tracyyun |
last post by:
Hello everyone,
I have a question and would like some advice on network connectivity. I have one computer connected to my router via WiFi, but I have two other computers that I want to be able to...
|
by: NeoPa |
last post by:
Hello everyone.
I find myself stuck trying to find the VBA way to get Access to create a PDF of the currently-selected (and open) object (Form or Report).
I know it can be done by selecting :...
|
by: isladogs |
last post by:
The next Access Europe meeting will be on Wednesday 1 Nov 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM)
Please note that the UK and Europe revert to winter time on...
|
by: NeoPa |
last post by:
Introduction
For this article I'll be focusing on the Report (clsReport) class. This simply handles making the calling Form invisible until all of the Reports opened by it have been closed, when it...
|
by: isladogs |
last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM).
In this month's session, Mike...
| |