| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"david wolf" |
| Date: |
29 Mar 2006 08:15:57 AM |
| Object: |
about static variable of a class |
It is said that in Efffective C++ book that if a static varaible is
declared inside a class (in this case class A), then a separate
definition of it should be placed outside of the header file (say
z1.cpp). But I did not do that, the program can still compile and run.
As a matter of fact, if I put a definition line (as suggested by the
author):
const double A::a1;
immediately before the main() of z1.cpp, it also compiles and runs too.
Can someone tell me why?
--David
-------------------------contents of z1.h------------------------
class A{
private:
static const double a1 = 1.3;
public:
double getA(){
return a1;
}
};
-------------------------contents of z1.cpp -----------------------
#include "z1.h"
#include <iostream>
int main(){
A tmpa;
cout<<tmpa.getA()<<endl;
}
------------------------------running result --------------------
$ g++ z1.cpp
$ a.out
1.3
.
|
|
| User: "Jaspreet" |
|
| Title: Re: about static variable of a class |
29 Mar 2006 06:22:57 PM |
|
|
david wolf wrote:
It is said that in Efffective C++ book that if a static varaible is
declared inside a class (in this case class A), then a separate
definition of it should be placed outside of the header file (say
z1.cpp). But I did not do that, the program can still compile and run.
As a matter of fact, if I put a definition line (as suggested by the
author):
const double A::a1;
immediately before the main() of z1.cpp, it also compiles and runs too.
That is the correct behavior, I suppose. It initialises the variable to
0.
Can someone tell me why?
--David
-------------------------contents of z1.h------------------------
class A{
private:
static const double a1 = 1.3;
public:
double getA(){
return a1;
}
};
Are you sure this compiles ? I thought you could initialise only an int
in that manner. Try using gcc -Wall -ansi (or some switch which does
strong typechecking). Probably your compiler extensions are enabled.
-------------------------contents of z1.cpp -----------------------
#include "z1.h"
#include <iostream>
int main(){
A tmpa;
cout<<tmpa.getA()<<endl;
How did this line compile ? You need to use "std::" before cout and
endl.
Instead of endl it would be preferable to use "\n".
}
------------------------------running result --------------------
$ g++ z1.cpp
$ a.out
1.3
.
|
|
|
| User: "david wolf" |
|
| Title: Re: about static variable of a class |
30 Mar 2006 04:19:50 PM |
|
|
I am using redhat 7.3's gcc compiler. It compile and runs fine. That is
why I am curious about.
.
|
|
|
| User: "Ian Collins" |
|
| Title: Re: about static variable of a class |
30 Mar 2006 05:22:59 PM |
|
|
david wolf wrote:
I am using redhat 7.3's gcc compiler. It compile and runs fine. That is
why I am curious about.
Please quote some context.
I don't think your original example (see why context is good?) should
have compiled.
If a1 had been an integer type you would have been OK. Unless you try
and use its address, for example:
struct X
{
static const int d = 42;
};
int main()
{
int d = X::d;
const int* dp = &X::d;
}
Will fail.
--
Ian Collins.
.
|
|
|
|
|
|
| User: "Puppet_Sock" |
|
| Title: Re: about static variable of a class |
29 Mar 2006 09:48:36 AM |
|
|
david wolf wrote:
It is said that in Efffective C++ book that if a static varaible is
declared inside a class (in this case class A), then a separate
definition of it should be placed outside of the header file (say
z1.cpp). But I did not do that, the program can still compile and run.
As a matter of fact, if I put a definition line (as suggested by the
author):
const double A::a1;
immediately before the main() of z1.cpp, it also compiles and runs too.
Can someone tell me why?
--David
-------------------------contents of z1.h------------------------
class A{
private:
static const double a1 = 1.3;
public:
double getA(){
return a1;
}
};
-------------------------contents of z1.cpp -----------------------
#include "z1.h"
#include <iostream>
int main(){
A tmpa;
cout<<tmpa.getA()<<endl;
}
------------------------------running result --------------------
$ g++ z1.cpp
$ a.out
1.3
Are you sure that compiles? I thought only const static integral
data could in initialized that way. const integral static data is
supposed to be able to be init-ed in the class. (IIRC anyway.)
Maybe it's a compiler extension?
Socks
.
|
|
|
|
| User: "Phlip" |
|
| Title: Re: about static variable of a class |
29 Mar 2006 08:26:47 AM |
|
|
david wolf wrote:
It is said that in Efffective C++ book that if a static varaible is
declared inside a class (in this case class A), then a separate
definition of it should be placed outside of the header file (say
z1.cpp). But I did not do that, the program can still compile and run.
static const double a1 = 1.3;
Because it's const. The compiler is free to insert 1.3 everywhere you write
a1, so a1 probably doesn't need a unique spot storage.
--
Phlip
http://www.greencheese.org/ZeekLand <-- NOT a blog!!!
.
|
|
|
| User: "david wolf" |
|
| Title: Re: about static variable of a class |
29 Mar 2006 08:55:26 AM |
|
|
I think you are not right, Phlip. Definitely it needs a sport storage.
Compiler is not preprocessor. It should give it a storage anyway.
.
|
|
|
|
|

|
Related Articles |
|
|