frei@mez.e-technik.uni-bochum.de (Matthias Frei) (09/19/89)
I've a question to the scope of static members in Rel. 2.0. On page 441 lippman provides an example on static member initialization: class Y { ... stuff deleted private: static int val; }; int Y::val = 1; Is the statement Y::val = 1 a bug in the book, or are the scope rules from Rel 1.2 invalid ? I think val is private to Y and is seen inside the class only. If that statement is legal, I hope it only may occur once near by the class definition. Or can I assign in this way everywhere ? Could somebody please clarify that point ? clarify that point ? Matthias -- Matthias Frei | Snail-mail: | Microelectronics Center frei@mez.e-technik.uni-bochum.de | University of Bochum, W-Germany frei%rubmez@unido.BITNET | D-4630 Bochum, P.O.-Box 102143
ark@alice.UUCP (Andrew Koenig) (09/20/89)
In article <292@mez.e-technik.uni-bochum.de>, frei@mez.e-technik.uni-bochum.de (Matthias Frei) writes: > I've a question to the scope of static members in Rel. 2.0. > On page 441 lippman provides an example on static member initialization: > > class Y { > ... stuff deleted > private: > static int val; > }; > int Y::val = 1; > Is the statement Y::val = 1 a bug in the book, or are the scope rules > from Rel 1.2 invalid ? I think val is private to Y and is seen inside > the class only. The book is correct. > If that statement is legal, I hope it only may occur once near by > the class definition. Or can I assign in this way everywhere ? It may occur only once in the entire program, although that once may be anywere. It is exactly analagous to this: class X { private: void f(); }; void X::f() { /*...*/ } Surely you won't object to the definition of the function member X::f even though it's private. Why, then, should you object to a similar definition of a data member? -- --Andrew Koenig ark@europa.att.com
jima@hplsla.HP.COM (Jim Adcock) (09/21/89)
>ark: >It may occur only once in the entire program, >although that once may be anywere. ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ ***sigh***, if this statement were only true! See below: #include <stdio.h> class Y; //const int Y::size = 100; 2.0 compiler won't accept it here class Y { //const int Y::size = 100; 2.0 compiler won't accept it here static const int Y::size; //const int Y::size = 100; 2.0 compiler won't accept it here int array[size]; }; const int Y::size = 100; // 2.0 compiler will accept it here, but its too late! void main() { printf("sizeof(Y)=%d\n",sizeof(Y)); }
jenings@hpfclp.SDE.HP.COM (Byron T. Jenings Jr.) (09/21/89)
That isn't an assignment, it's a definition. You can only have one
definition like that in the entire program, just like any other variable
definition. Assignments to the members will still generate "private
member" errors, as you'd expect. It is similar in concept to declaring
a member function, and then specifying the definition of the function
body elsewhere.
The C translation of your example should clarify:
struct Y {
int deleted__1Y;
};
extern int val__1Y; // The static member isn't defined yet,
// just declared.
int val__1Y = 1; // Here's the definition
marc@dumbcat.UUCP (Marco S Hyman) (09/21/89)
In article <292@mez.e-technik.uni-bochum.de> frei@mez.e-technik.uni-bochum.de (Matthias Frei) writes: class Y { ... stuff deleted private: static int val; }; int Y::val = 1; Is the statement Y::val = 1 a bug in the book, or are the scope rules from Rel 1.2 invalid ? As ark has responded it's not an error. The thing to remember is that ``The declaration of a static member in its class declaration is *not* a definition.'' That quote is from the AT&T C++ Release 2.0 Product Reference Manual. --marc -- // Marco S. Hyman {ames,pyramid,sun}!pacbell!dumbcat!marc
dattatri@metaphor.Metaphor.COM (Dattatri) (03/05/91)
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? Thanks in advance Kayshav -- dattatri@metaphor.com
rfg@NCD.COM (Ron Guilmette) (03/10/91)
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.