static member variable
In order to share data in different objects, and not destroy the principle of data hidden, keep safe. So static member is shared by all the objects of a class, and not the member of a given object. And it can save memory, as it only save in one location to shared by all the objects. The member can also be updated, we can ensure that the objects read the updated value, it's worthy.
To use a static member, we should pay attenen to
1. use static keyword when in definition
2. there is different between static member initialization and common data member initialization
<data type> <classname>::<static member name>=<value>
it means,
Initialization should be out of class, no static or private/public here;
The static member should be initialized before using, we should use the style <class name>::<static member name> to initialize.
If the memeber is public, we chould modify the value as the format above.
static member function
The same as static member variable, both are not belong to object, but class. So, we don't need object name to reference. We could not get nonstatic member variable directly, but we could get static member variable directly. Static member function will not use the default this pointer, it's why it could not use the nonstatic member variable. Maybe in sometimes, we sould use static function to read nonstatic member variable. Maybe in multithread, in fact, I'm not familiar with that. We said static member function don't have this pointer, why not pass this pointer by ourselves? We could write a function to return the objects address(pay attention to object), and we will get what we want. It only simulate the behavior of pass this pointer.
We could use static member in order to
1. save the number of objects (it's useful sometimes, as you want to know how many objects is generated)
2. as a flag to show status (we could know how something is working now, as file is open or not, printer is using or not)
3. save the first or the last address in a linked list
A simple example to use static member variable and static function
#pragma once
#include < iostream >
class Object
... {
public:
Object()
: name("")
...{
number++;
}
Object(const std::string &name)
: name(name)
...{
number++;
}
// this function is only to simulate this pointer
const Object & pointer() const
...{
return (*this);
}
// static function to read static variable
static int getNumber()
...{
return number;
}
// only to show how to read nonstatic variable in a static function
static const std::string & getName(const Object & object)
...{
return object.pointer().name;
}
private:
std::string name;
private:
// set to private to prevent modified
static int number;
} ;
#include " Object.h "
int Object::number = 0 ;
#include < iostream >
#include " Object.h "
using namespace std;
int main ( int argc, char * argv[])
... {
Object a("vincent");
cout<<a.getNumber()<<endl;
Object c,d("Jim"),e,f;
cout<<c.getNumber()<<endl;
cout<<d.getNumber()<<endl;
cout<<a.getNumber()<<endl;
// a.getName(a) and b.getName(a) are both OK, but not recommended
cout<<Object::getName(a).c_str()<<endl;
system("pause");
return 0;
}