Recently see effective C + + often see some and static related content, hope that a comprehensive collation, if not all, also hope to add:
Static member variables in Class 1
Static members Unlike normal data members, static data members exist independent of any object of the class, and each static data member is an object associated with the class and is not associated with an object of that class!
It is generally requested for a class to request a copy of a member variable in a class, the member variables and functions between the individual objects do not affect each other, but the static member variable is not in the stack space but in the static store, and all class objects share the static variable. static members can be accessed independently , without creating any objects to access
Just define a static variable that cannot be initialized in the class, including the constructor of the class, and the initialization of the static member is done outside the class.
A static data member is actually a global variable in a class domain. Therefore, the definition of a static data member (initialization) should not be placed in the header file (nor in the VS error).
Note: do not attempt to define (initialize) static data members in the header file. In most cases, doing so would cause a duplicate definition of such an error. Even adding #ifndef #define #endif或者 #pragma once.
#include "static.h" #include <iostream>using namespace std;class person{private:string name;static int age;public :P Erson (const STRING&NM): Name (nm) {}void Print () {cout<<name<< "is" <<age<<endl;}}; int Person::age=20;int Main () {person person ("Tom");p Erson. Print (); Cout<<endl;return 0;}
Static members can only be accessed by the static member function
The static member can implement encapsulation, for Public,private,protect
static members are required to be defined outside the class;
Note: aconst static int member can be defined inside a class because it is a const member and cannot declare a const static member of another type in a class other than int
const static int age=20; Okconst static string address= "XT"; Error
The type of a static data member can be the type of the owning class, while a normal data member cannot. A normal data member can only be declared as a pointer or reference to the owning class type. Examples are as follows:
Static member functions in Class 2
Because the static member does not belong to an object in the class, the static member function does not have the this pointer, and the general member function can be declared as a const description function does not modify the owning class
Information, thus, without this pointer, a static member function cannot be declared as const
The static member function only has access to static members that cannot use non-static members
Class person{private:string name;const static int age=20; static string Address;public:person (const STRING&NM): Name (nm) {}static void Print () {cout<< "name is" << age<<endl;cout<< "Adress is" <<address<<endl;}}; String person::address = "XT"; <pre name= "code" class= "CPP" > const int person::age;//< Although already initialized, the definition is given (c + + Primer must be defined, but it is not a problem to compile and run in vs.
One of the const static member functions: Here should be a bit confusing, in fact, the return type is const
The address of a static member function can be assigned to a normal function pointer store, the ordinary member function address can only be assigned to the class member pointer
3 Inheritance
Static member variables are also shared by class-derived class objects
#include "static.h" #include <iostream>class base{public:static int _num;//Declaration}; int base::_num=0;//The true definition of a static data member class Derived:public base{}; void Main () {base A; derived b; a._num++; cout<< "base class static data number _num is" <<a._num<<endl; b._num++; cout<< "derived class static data number _num is" <<b._num<<endl; }
Output is
const static int age=20; Const static string address= "XT";
Static member variable, static member function parsing