 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Bhavesh Goswami Guest
|
Posted: Mon Mar 28, 2005 11:57 am Post subject: static data type and initialization issues |
|
|
Hey,
I was surprised to find recently that you cannot use static datatype
without initializing it (I though it was automatically initialized to
zero). The below is a very small sample program from a c++ book..
_______
#include <stdio.h>
class gamma{
private:
static int total;
int id;
public:
gamma()
{
total++;
id = total;
}
static void showtotal()
{
printf("nTotal is %dn", total);
}
};
int gamma::total = 0; //the program will not compile without this line
void main()
{
gamma g1;
g1.showtotal();
}
___________________
If I remove the line "int gamma::total=0;", then the .NET complier and
also gcc compiler on solaris gives linker error like "cannot find
gamma::total". I referenced two book and previous posts but cannot find
explanation on why this might be happening.. Any inputs to help me
understand the whole deal will be really appreciated.
Thanks,
Bhavesh
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Steffen Jakob Guest
|
Posted: Mon Mar 28, 2005 5:22 pm Post subject: Re: static data type and initialization issues |
|
|
On Mon, 28 Mar 2005 06:57:39 -0500, Bhavesh Goswami wrote:
| Quote: | Hey,
I was surprised to find recently that you cannot use static datatype
without initializing it (I though it was automatically initialized to
zero). The below is a very small sample program from a c++ book..
|
// ...
| Quote: | If I remove the line "int gamma::total=0;", then the .NET complier and
also gcc compiler on solaris gives linker error like "cannot find
gamma::total". I referenced two book and previous posts but cannot find
explanation on why this might be happening.. Any inputs to help me
understand the whole deal will be really appreciated.
From the ISO Standard:
|
9.4.2: "The declaration of a static data member in its class definition is
not a definition and may be of an incomplete type other than cv-qualified
void. The definition for a static data member shall appear in a namespace
scope enclosing the member's class definition. In the definition at
namespace scope, the name of the static data member shall be qualified by
its class name using the :: operator." ...
Greetings,
Steffen.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Peter C. Chapin Guest
|
Posted: Mon Mar 28, 2005 5:25 pm Post subject: Re: static data type and initialization issues |
|
|
"Bhavesh Goswami" <bhavesh0007 (AT) yahoo (DOT) com> wrote in
news:1111975409.303963.194750 (AT) g14g2000cwa (DOT) googlegroups.com:
| Quote: | If I remove the line "int gamma::total=0;", then the .NET complier and
also gcc compiler on solaris gives linker error like "cannot find
gamma::total". I referenced two book and previous posts but cannot
find explanation on why this might be happening.. Any inputs to help
me understand the whole deal will be really appreciated.
|
It's not the explicit initialization that it needs, but rather the
definition. When you declare a static data member of a class, it is only a
*declaration*. To entice the compiler to actually set aside storage for the
static member you must *define* it. That's the purpose of the line the
compiler complains about when you remove.
Peter
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Thomas Maeder Guest
|
Posted: Mon Mar 28, 2005 5:33 pm Post subject: Re: static data type and initialization issues |
|
|
"Bhavesh Goswami" <bhavesh0007 (AT) yahoo (DOT) com> writes:
| Quote: | I was surprised to find recently that you cannot use static
datatype without initializing it (I though it was automatically
initialized to zero). The below is a very small sample program from
a c++ book..
|
<picking nits>
Your question is not about a static type, but about a static object.
</picking nits>
| Quote: | class gamma{
private:
static int total;
|
This is the declaration of a static data member. It tells the public
that if a member named total of class gamma is accessed, it's static
and of type int.
It's no definition, though. No memory is allocated for gamma::total.
| Quote: | };
int gamma::total = 0; //the program will not compile without this line
|
This is the definition the the static data member gamma::total. The
member can only be accessed (or have its address taken) if the program
contains such a definition in one of the translation units.
This definition is equivalent to
int gamma::total;
because, as you write, static data is zero-initalized by default.
A Standard conforming compiler should issue a diagnostic here. The
return type of main() has to be int.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
comp.ai.games Guest
|
Posted: Mon Mar 28, 2005 11:51 pm Post subject: Re: static data type and initialization issues |
|
|
Thanks to all those who replied. A part of my question was that I
referenced a few books including, "Turbo C++" by Robert Lafore and
"Teach yourself C++ ...." by Jesse liberty and also a few web resources
and they have no mention of such a thing. Infact the code mentioned in
the post comes straight out of a book (with some pruning for
simplicity). Is this a recent amendment or is it compiler and books
catching up with actual standards ???
Thanks
Bhavesh
Ps: on other note, is the need for main to have return type int a
standard or good practice ?
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Shantanu Garg Guest
|
Posted: Tue Mar 29, 2005 9:31 am Post subject: Re: static data type and initialization issues |
|
|
| Quote: | Ps: on other note, is the need for main to have return type int a
standard or good practice ?
|
'main' should have a return type of 'int' and tt is in standard.
On the otherhand, you dont need to explicitely return a value from
'main'. A default one is generated implicitely.
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
|