Structs and Classes Static Class Members Recall the following A static global object (i.e., an object with a file scope) has no external linkage, i.e., it cannot be in any other file A static local object (i.e., an object with a function or a block scope) is not destroyed upon function or block exit A static data member of a class is shared by all instantiated objects of the class (all objects of the class share a single instance of the static data member) Class X { int j; // every object of class X have have its own instance of j static int I; // A static data member is declared, only one instance of I // will be defined and shared by all objects of class X. } // the static member I is only declared above and must be defined in a .cpp // file before it can be used (in X.cpp or in main_program.cpp) as follows
Structs and Classes Static Class Members(cont.) int X::I = 10;//the instance of I is defined and initialized, if the member is not // initialized, it will be initialized to zero by default main(){ X a,b,c; // instantiate three objects of type X a.j = 100; b.j = 10; c.j = 5; // assume j is in the public section of X a.I = 10; // also assumes s I is public in X, //the above is the same as b.I=10; or c.I=10 since I is shared by all objects X::I=100;// the class name can also be used to reference I } X::I j = 100 j = 10 j = 5 a b c
Structs and Classes Static Class Members(cont.) can be used for 1. Storing constant values or structures used by objects of the class 2. Passing data from any object of the class to other objects of the class 3. Storing results computer by collaboration among objects of the class The above considerably reduce the need for global variables Example: Fixed lenght vectors Class Vector{……. static const int lenght; // the declaratoin of length …..} const int Vector::length = 100;// the definition of length Vector::Vector(int n = length) {…….} The above shows that a static data member can appear as a default argument of a member function
Structs and Classes Static Class Members(cont.) Example: Counting the number of objects class X{… static int count; public: X(){…..count++;…..} static int get_count(){return count;}// a static member function is a // function which may only access static data members } main(){ X a,b,c; cout << “no. of objects created =\t” << X::get_count(); // Note a static member function can be invoked using the class name
Structs and Classes Static Class Members(cont.) A static data member can be an object of the class in which it is a member Class X{…. X* ptr; // OK X& ref; // OK X obj; // Error, a data member of a class can not have the class type static X obj; // OK since obj is a static data member } Example:The human race class class Human{…. static Human Adam, Eve; // these static objects are shared by all objects // of this class A pointer to a static data member is defined as follows Human * ptr_to_Adam = & Human::Adam;
Structs and Classes The this pointer A special pointer used in member functions of a class as a pointer to the object instance for which the member function was invoked. Class X { …. int I; public: int get_I(){return I;}// I can be referenced by this->I X increment_I(){ I++; return (*this);}// the pointer is implicitly declared // in every member of the class by X* const this } void f(X a, X b){… int I = a.get_I(); int j = b.get_I(); cout << a.increment_I().get_I(); }// a.increment returns back the instance of object a // Member functions utilizing “this” must be non-static
Structs and Classes The this pointer (cont.) A doubly linked list head prev next Class Dlink { void* item; Dlink* prev; Dlink* next; public: void insert_before_me(Dlink* p){ p->next = this; p->prev = prev; prev->next = p; prev = p; } void append_to_me(Dlink* p){p->next = next; p->prev = this; next->prev = p; next = p;} }; Dlink* head = new Dlink; viod fun(Dlink* x, Dlink* y){ head->insert_before_me(x); // will this work? // what changes needed to be done in the member function insert_before_me()? head->append_to_me(y); //will this work? // what changes needed in member function append_to_me()