[C++ basic knowledge] static members, a new method of member initialization in C++11

static member

Class members declared as static are called static members of the class, member variables modified with static are called static member variables ; member functions modified with static are called static member functions. Static member variables must be initialized outside the class

characteristic:

1. Static members are shared by all class objects and do not belong to a specific instance
. 2. Static member variables must be defined outside the class , without adding the static keyword when defining them
. 3. Class static members can use class names:: static members or Objects. Static members to access
4. Static member functions have no hidden this pointer and cannot access any non-static members.
5. Static members, like ordinary members of a class, also have three access levels: public, protected, and private, and they can also return
6. Static member functions cannot call non-static member functions, but non-static member functions can call static member functions.

A new method of initializing the number of members in C++11

#define _CRT_SECURE_NO_WARNINGS 1
#include<iostream>
using namespace std;
class A {
public:
	A(int a = 1) :_a()
	{
		
	}
	void print()
	{
		cout << this->_a << endl;
	}
private:
	int _a=0;		//C++11的成员初始化新方法
					//此处的缺省值只有在无其他值可用时才会使用
					//仅限于非static成员
};

int main()
{
	A x;
	x.print();
	return 0;
}

Guess you like

Origin blog.csdn.net/weixin_43962381/article/details/114375534