- Modern C++:Efficient and Scalable Application Development
- Richard Grimes Marius Bancila
- 553字
- 2021-06-10 18:28:15
Defining static members
When you use static on a class member it means that the item is associated with the class and not with a specific instance. In the case, of data members, this means that there is one data item shared by all instances of the class. Likewise, a static method is not attached to an object, it is not __thiscall and has no this pointer.
A static method is part of the namespace of a class, so it can create objects for the class and have access to their private members. A static method has the __cdecl calling convention by default, but you can declare it as __stdcall if you wish. This means that, you can write a method within the class that can be used to initialize C-like pointers, which are used by many libraries. Note that the static function cannot call nonstatic methods on the class because a nonstatic method will need a this pointer, but a nonstatic method can call a static method.
A nonstatic method is called through an object, either using the dot operator (for a class instance) or the -> operator for an object pointer. A static method does not need an associated object, but it can be called through one.
This gives two ways to call a static method, through an object or through the class name:
class mytype
{
public:
static void f(){}
void g(){ f(); }
};
Here, the class defines a static method called f and a nonstatic method called g. The nonstatic method g can call the static method, but the static method f cannot call the nonstatic method. Since the static method f is public, code outside the class can call it:
mytype c;
c.g(); // call the nonstatic method
c.f(); // can also call the static method thru an object
mytype::f(); // call static method without an object
Although the static function can be called through an object, you do not have to create any objects at all to call it.
Static data members need a bit more work because when you use static it indicates that the data member is not part of an object, and usually data members are allocated when an object is created. You have to define static data members outside of the class:
class mytype
{
public:
static int i;
static void incr() { i++; }
};
// in a source file
int mytype::i = 42;
The data member is defined outside of the class at file scope. It is named using the class name, but note that it also has to be defined using the type. In this case the data member is initialized with a value; if you do not do this, then on the first use of the variable it will have the default value of the type (in this case, zero). If you choose to declare the class in a header file (which is common), the definition of the static data members must be in a source file.
You can also declare a variable in a method that is static. In this case, the value is maintained across method calls, in all objects, so it has the same effect as a static class member, but you do not have the issue of defining the variable outside of the class.