[CSI] static non-const class members

Mikhail Nesterenko mikhail at cs.kent.edu
Tue Mar 31 14:30:32 EDT 2020


CSI students,

At the beginning of the class today, I was talking about static
class members. I modified this friend example

  http://vega.cs.kent.edu/~mikhail/classes/csi/Examples/Classes/friend.cpp

for data_ to be a static constant. Like so:

    static const int data_; 

This did not work because data_ is modified in function set:

     void set(int data) { data_ = data; }

Then I tried to make data_ a variable. Like so:

    static int data_;

Unfortunately, this would not compile, because static variables need
to be initialized outside the class. A line like this needs to be
added for the program to compile:

   int MyClass::data_ = 55;

In CSI, we do not study static member variables, only static
constants.  I added an example for static constant use to the class
examples on the course's webpage here:

  http://vega.cs.kent.edu/~mikhail/classes/csi/Examples/Classes/

Thanks,
-- 
Mikhail


More information about the cs13001 mailing list