C ++: static class

static self-understanding

such that static data members or functions for the life cycle of the entire life of the application file is located, in C, it may also be prevented from being used with other external files member

static class

Clear: static data members of a class that is shared by all class objects, but it belongs to the class, rather than the object of the class

Static members are: static occurs only inside the class declaration statement

Static member function in the internal and external classes can be defined (only need to add static before the function declaration)

class a1{
....
static void func1();
};
void  a1::func1(){...}

Because static data members do not belong to any object class, which means they are not a class constructor to initialize must be outside the class
and can only be defined once and initialize each define static data members

And preferably defined in a file with other non-inline function in

Class initialization method:

We can provide the type of class within the initial values ​​for the static const member integer, but requires a static member must be a literal type of constexpr

eg: static constexpr int period = 30;

Static members can be used in certain scenarios:

Static data members may be incomplete types:
EG:

class Bar{
    public:...
    private:
    static Bar mem1;  //OK
    Bar *mem2; //OK
    Bar mem3; //ERROR
};

We can use a static member as the default argument
eg:

class Screen{
public:
Screen & clear(char = bkground)
private:
  static const char bkground;
};

Guess you like

Origin www.cnblogs.com/Liberavi/p/11568543.html