


Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
The concept of static members in c++ classes, including their declaration, definition, initialization, and usage. Static members belong to the whole class instead of individual objects and can be accessed through the class scope operator. This document also covers private static members and static member functions, which provide access to private static variables. Examples and summarizes the key properties of static members and functions.
What you will learn
Typology: Study notes
1 / 4
This page cannot be seen from the preview
Don't miss anything!
David Kieras Prepared for EECS 381, Fall 2004 Non-static (ordinary) member variables Regular member variables of a class exist in every object. That is, when you declare a class and list the member variables, you are saying that every object in the class should have its own space in memory for each member variable. class Thing { public: int x; int y; }; ... int main() { Thing t1, t ... After the declaration of Things t1 and t2, each consists of a piece of memory holding two integers, so we have t1.x, t1.y, t2.x, t2.y. Each object has its own individual x and y variables. Static member variables Sometimes it is handy to have a member variable that is associated with the whole class instead of individual objects. Such a variable occupies its own piece of memory, by itself, instead of being part of an individual object. It is essentially a global variable, but its name is contained inside a class scope, so it goes with the class instead of being known everywhere in the program. Such a member variable can be made private to a class, meaning that only member functions can access it. A good name for this property would be something like "class-wide" or "whole-class", but unfortunately, the overworked keyword "static" was used instead, so we have "static member variables". Here is an example of a Thing class where a static member variable, named "count", is used to keep a count how many Things exist. Every time a Thing is created, the constructor will increment the count; every time a Thing is destroyed, the destructor will decrement the count. Because the count variable is not part of individual objects, but exists for the entire class, it "lives" past the creation and destruction of individual objects, retaining its value while the individual objects come and go. Example: class Thing { public: Thing () {count++;} // there is now one more Thing ~Thing () {count--;} // there is now one fewer Thing int x; int y;: static int count; }; In this example, count is a public static member variable whose full name is Thing::count. Even if it was private, the constructor and destructor member functions can access it because they are member functions. Because count is static, it is a class-wide member variable, not a member
variable of the individual thing objects; there is only one Thing::count variable in the entire program. You have to define and initialize a static member variable somewhere in your code, at the top level (outside of any function), the same way you define and initialize a global variable. The compiler will ensure that the initialization is done before your program starts executing at the main function. In this definition and initialization line, you provide the full name of the static member variable using the class scope operator: The best place to define the static member variable is near the beginning of the .cpp file that goes with the header file that contains the class declaration. For example in the file "Thing.h", we have the Thing class declaration, and then in "Thing.cpp" we would have: #include "Thing.h" other includes, etc. int Thing::count = 0; // define the static member variable code for Thing class member functions Actually, the "= 0;" is optional; by default static variables are initialized to whatever type of zero is appropriate, but including it is a customary way of making this definition more obvious. At any point in your code, you can refer to a static member variable either in the context of an object, or just with the class scope operator, assuming that the code has access: Thing t; ... x = t.count; // get the count x = Thing::count; // get the count You don't have to have an object involved because the static member variable belongs to the whole class. If you access the static member variable with an object, like in "t.count", the only role the object "t" plays is to inform the compiler what class the "count" variable belongs to - count is not part of the object, but belongs to the whole class of objects. Actually, accessing a static member variable through an object is not usually done; the class scope operator form is usually how it is done, or because the variable is normally private, through a static accessor function (see below). Defining and initializing private static member variables You can make a static member variable private, and often want to. But you can (and must) still define it the same way as shown above, even though it is private. For example, in Thing.h: class Thing { public: Thing () {count++;} ~Thing () {count--;} int x; int y; private: static int count; };
In your code, call the function as follows: Thing t; ... x = t.get_count(); // get the count x = Thing::get_count(); // get the count The function get_count is a static member function that returns the value of the static member variable count. Because it is static, it can be called either with or without an object being involved. If it is called with an object, as in t.get_count(), the only role that object "t" plays is to inform the compiler what class get_count belongs to. As with static member variables, accessing a static member function through an object is not usually done. Normally, the class scope operator form is used to call a static member function. An important property of a static member function is that you can't access the ordinary (non-static) members of the class from within the static member function - there is no individual object automatically involved (there is no "this" object). If you need your static member function to access the members of an object in its class, you have to pass in the object as a parameter of some sort, or provide some other access (e.g. through a global variable or static member variable). Because the static member function is a member of the class, it can then access all members of the passed-in object. Summary A static member variable: