Berkeley CSUA MOTD:Entry 25622
Berkeley CSUA MOTD
 
WIKI | FAQ | Tech FAQ
http://csua.com/feed/
2025/05/25 [General] UID:1000 Activity:popular
5/25    

2002/8/20-21 [Computer/SW/Languages/Java] UID:25622 Activity:low
8/20    How do you declare static array in C++ class?
        \_ you mean something like this?
           http://www.icce.rug.nl/documents/cplusplus/cplusplus10.html
2025/05/25 [General] UID:1000 Activity:popular
5/25    

You may also be interested in these entries...
2013/5/1-18 [Computer/SW/Languages/Java, Computer/Theory] UID:54669 Activity:nil
5/1     What's the difference between CS and Computer Engineering?
        http://holykaw.alltop.com/top-ten-paying-degrees-for-college-graduates
        \_ One is science and the other is engineering.
        \_ From http://en.wikiquote.org/wiki/Computer_science
           'A folkloric quotation ... states that "computer science is no more
           about computers than astronomy is about telescopes."  The design
	...
2012/12/4-18 [Computer/SW/Languages/Java] UID:54544 Activity:nil
12/4    Holy cow, everyone around me in Silicon Valley is way beyond
        middle class according to Chinni's definition:
        http://en.wikipedia.org/wiki/American_middle_class
        \_ Let's set our goals higher:
           http://en.wikipedia.org/wiki/Upper_middle_class_in_the_United_States
           \_ How about this one?
	...
2012/10/29-12/4 [Science/Disaster, Computer/SW/Languages/Java, Politics/Domestic/President/Bush] UID:54516 Activity:nil
10/29   Go Away Sandy.
        \_ Sorry, Coursera is performing preventive maintenance for this
           class site ahead of Hurricane Sandy. Please check back in 15 minutes.
           class site ahead of Hurricane Sandy. Please check back in 15
           minutes.
        \_ Bitch.
	...
2012/1/18-3/3 [Computer/SW/Languages/Java, Finance/Investment] UID:54290 Activity:nil
1/18    I own a bunch of NFLX stocks bought at several different periods
        (from high $200 all the way down to $80). I dumped a few and
        still have a few. Why the hell is Reid Hastings still making
        $500,000/year? How do I join the pending NFLX Class Action
        Lawsuit?
        \_ Why would you buy stock in a company run by a narcissistic
	...
Cache (7266 bytes)
www.icce.rug.nl/documents/cplusplus/cplusplus10.html
All mail received is seriously considered, and new (sub)releases of the Annotations will normally reflect your suggestions for improvements. Except for the incidental case, I will normally not acknowledge the receipt of suggestions for improvements. Please don't interpret this as me showing lack of appreciation. In the previous chapters we have shown examples of classes where each object of a class had its own set of public or private data. Each public or private function could access the members of objects of that class. In some situations it may be desirable that one or more common data fields exist, which are accessible to all objects of the class. An example of such a situation is the name of the startup directory in a program which recursively scans the directory tree of a disk. A second example is a flag variable, which states whether some specific initialization has occurred: only the first object of the class would would perform the necessary initialization and would set the flag to done'. Such situations are analogous to C code, where several functions need to access the same variable. A common solution in C is to define all these functions in one source file and to declare the variable as a static: the variable name is then not known beyond the scope of the source file. This approach is quite valid, but doesn't stroke with our philosophy of one function per source file. Neither the first, nor the second C-like solution is elegant. C++'s solution is to define static members: data and functions, common to all objects of a class and inaccessible outside of the class. These functions and data will be discussed in this chapter. Such a data member is created and initialized only once, in contrast to non-static data members, which are created again and again, for each separate object of the class. Static data members are created when the program starts executing. Note, however, that they are always created as members of their classes. It is suggested to prefix static member names with s_ in order to distinguish them (in class member functions) from the class' data members. Static data members which are declared public are like normal' global variables: they can be reached by all code of the program by simply using their names, together with their class names and the scope resolution operator. This is illustrated in the following example: class Test { public: static int s_public_int; During the execution of the program, only one Directory::path exists, even though more than one object of the class Directory may exist. This data member could be inspected or altered by the constructor, destructor or by any other member function of the class Directory. Since constructors are called for each new object of a class, static data members are never initialized by constructors. The reason for this is that the static data members exist before any constructor of the class has been called. The static data members can be initialized during their definition, outside of all member functions, in the same way as global variables are initialized. At its implementation (definition) its type and class name are explicitly stated. Note also that the size specification can be left out of the interface, as is shown in the above array path . However, its size is (either explicitly or implicitly) needed at its definition. Note that any source file could contain the definition of the static data members of a class. Of course, any source file definining static data of a class must also include the header file of that class, in order for the static data member to be known to the compiler. A second example of a useful private static data member is given below. The initialization of the device, which in this case would be to switch from text mode to graphics mode, is an action of the constructor and depends on a static flag variable nobjects. The variable nobjects simply counts the number of Graphics objects which are present at one time. Similarly, the destructor of the class may switch back from graphics mode to text mode when the last Graphics object ceases to exist. The class interface for this Graphics class might be: class Graphics { static int s_nobjects; When the first object is created, the graphics device is initialized. At the destruction of the last Graphics object, the switch from graphics mode to text mode is made: int Graphics::s_nobjects = 0; As before, the class interface would only declare the array s_path . This means that some source file would still need to contain the definition of the s_path array. Similar to the concept of static data, in which these variables are shared by all objects of the class, static member functions exist without any associated object of their class. Static member functions can access all static members of their class, but also the members (private or public) of objects of their class if they are informed about the existence of these objects, as in the upcoming example. Static member functions are themselves not associated with any object of their class. In fact, a static member function is completely comparable to a global function, not associated with any class. This implies that the address of a static member function could be used in functions having parameters that are pointers to (global) functions. Since static member functions are comparable to ordinary global functions, static member functions that are declared in the public section of a class interface can be called without specifying an object of the class. The following example illustrates these characteristics of static member functions: class Directory { string d_currentPath; Alternatively, a string or a pointer to dynamic memory could have been used. However, the object whose member must be modified is given to the member function as a reference parameter. But the compiler must know to which class the function belongs, so the class is mentioned, using the scope resolution operator. But here dir is used to tell the compiler that we're talking about a function in the Directory class. So, static member functions can be called as normal member functions. As in 4, the class and the scope resolution operator are used. But here dir is used to tell the compiler that we're talking about a function in the Directory class. Here in particular note that this is not using preset() as an ordinary member function of dir: the function still has no this-pointer, so dir must be passed as argument to inform the static member function preset about the object whose currentPath member it should modify. In the example only public static member functions were used. C++ also allows private static member functions: these functions can only be called by member functions of their class. Finally it is noted that static member functions cannot be defined as inline functions. Since a static function can be used as an ordinary function at the global level, it must have an address. The code of inline functions is normally directly inserted into the code of the program: not a true function, but an alias for a few statements. With inline functions we have no function address', Consequently, inline functions lack a basic requirement for static member functions: they have no address.