 |
C++Talk.NET C++ language newsgroups
|
| View previous topic :: View next topic |
| Author |
Message |
Jorge Martin-de-Nicolas Guest
|
Posted: Sun Feb 29, 2004 4:01 am Post subject: Static member initialization without class scope operator |
|
|
Hello,
Can somebody tell me why the following program works OK? I have tried
it and it compiles and runs OK...
------------------------------------------------------------
class A
{
public:
static int f();
static int myValue;
};
int A::myValue = f();
int
A::f()
{
return 55;
}
int
main(int argc, char* argv[])
{
A a;
std::cout << "myValue = " << a.myValue << std::endl;
}
------------------------------------------------------------
I would have expected to need the scope operator A::f() when
initializing A::myValue. How come just f() works? The program compiles
and works under Solaris, using both the Sun Workshop compiler and the
gcc compiler...
I've browsed my copy of Stroustrup Special Edition, but I can't find
an answer.
Thanks,
Jorge
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Stephen C. Dewhurst Guest
|
Posted: Sun Feb 29, 2004 4:47 pm Post subject: Re: Static member initialization without class scope operato |
|
|
On 28 Feb 2004 23:01:08 -0500, [email]jorgemdn (AT) yahoo (DOT) com[/email] (Jorge
Martin-de-Nicolas) wrote:
| Quote: | I would have expected to need the scope operator A::f() when
initializing A::myValue. How come just f() works? The program compiles
and works under Solaris, using both the Sun Workshop compiler and the
gcc compiler...
I've browsed my copy of Stroustrup Special Edition, but I can't find
an answer.
|
The initializer of myValue is essentially in the scope of class A.
See the standard, 3.3.6, point 5 in list, and 9.4.2. Note that this
won't work:
class A
{
public:
typedef int Int;
static Int f();
static Int myValue;
};
Int A::myValue = f(); // error!
Steve
Steve Dewhurst
www.semantics.org
[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
|
|
| Back to top |
|
 |
Gabriel Dos Reis Guest
|
Posted: Sun Feb 29, 2004 5:45 pm Post subject: Re: Static member initialization without class scope operato |
|
|
[email]jorgemdn (AT) yahoo (DOT) com[/email] (Jorge Martin-de-Nicolas) writes:
| Quote: | Hello,
Can somebody tell me why the following program works OK? I have tried
it and it compiles and runs OK...
------------------------------------------------------------
class A
{
public:
static int f();
static int myValue;
};
int A::myValue = f();
|
Standard mandated behaviour. 8.5/10:
An initializer for a static member is in the scope of the member's
class. [Example:
int a;
struct X {
static int a;
static int b;
};
int X::a = 1;
int X::b = a; // X::b = X::a
--end example]
--
Gabriel Dos Reis
[email]gdr (AT) cs (DOT) tamu.edu[/email]
Texas A&M University -- Computer Science Department
301, Bright Building -- College Station, TX 77843-3112
[ 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
|
|