Path: utzoo!news-server.csri.toronto.edu!rutgers!usc!elroy.jpl.nasa.gov!decwrl!infopiz!lupine!rfg From: rfg@NCD.COM (Ron Guilmette) Newsgroups: comp.lang.c++ Subject: Re: Static member Initialization Keywords: Questions? Message-ID: <4324@lupine.NCD.COM> Date: 10 Mar 91 02:22:47 GMT References: <2104@metaphor.Metaphor.COM> Organization: Network Computing Devices, Inc., Mt. View, CA Lines: 39 In article <2104@metaphor.Metaphor.COM> dattatri@metaphor.Metaphor.COM (Dattatri) writes: +Here is a piece of code. + +class st { + static int x; + int y; + public: + static display() { printf("x = %d\n", x); } + static increment() { x++; } +}; + +main() +{ + st::display(); // 1 + st s; + int st::x = 20; + st::display(); // 2 + int st::x = 30; + st::increment(); + s.display(); +} + +When I compile and run this code, what should be the value of 'x' at (1)? +At (1) is 'x' already initialized? + +With the g++, (1) prints x=30. +What is the effect of st::x=20? The fact that g++ allows this is a bug in g++. According to the ARM (section 9.4, page 180) "Static members of a global class are initialized exactly like global objects and ONLY AT FILE SCOPE." (The added emphasis is mine.) -- // Ron Guilmette - C++ Entomologist // Internet: rfg@ncd.com uucp: ...uunet!lupine!rfg // New motto: If it ain't broke, try using a bigger hammer.