C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

Static member initialization without class scope operator

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
Jorge Martin-de-Nicolas
Guest





PostPosted: Sun Feb 29, 2004 4:01 am    Post subject: Static member initialization without class scope operator Reply with 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();

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





PostPosted: Sun Feb 29, 2004 4:47 pm    Post subject: Re: Static member initialization without class scope operato Reply with quote



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





PostPosted: Sun Feb 29, 2004 5:45 pm    Post subject: Re: Static member initialization without class scope operato Reply with quote



[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
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
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


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.