Detailed explanation of static member variables and static member functions, explcit and implicit type conversion, and friend function () in C++

1.static static member (function)

1.1 Concept

A class member declared as static is called a class Static members, member variables modified with static ; usestatic member variable, call it
static modifies 's member function , which is called Static member function . Static member variables must be initialized outside the class

1.2 Features

 Static members are shared byall class objects and do not belong to a specific object. They are stored In static area

 Static member variables must be defined outside the class, and do not add < when defining a i=4>static keyword, just declared in the class

Static member variables cannot be given default values, because the default values ​​​​are given to the initialization list, and the initialization list initializes a certain object. Static member variables belong to the entire class, not to a specific object.

class A {

//静态成员变量在类中声明
public:
	static int Count1;
private:
	static int Count2;
};
//静态成员变量在类外定义并且初始化,
//不需要static修饰,但需要类名::的形式指定类域
int A::Count1 = 1;	
int A::Count2 = 2;
Class static members are available Class name :: Static members Or object . static member to access
Static members are also members of the class, protected public , protected , private Restrictions on access qualifiers
class A {
public:
	void abc1()
	{
		//A::public静态成员变量可以在类内表示
		cout << A::Count1<<endl;
	}
	void abc2()
	{
		//A::private静态成员变量可以在类内表示
		cout << A::Count2 << endl;
	}
//静态成员变量在类中声明
public:
	static int Count1;
private:
	static int Count2;
};
//静态成员变量在类外定义并且初始化,
//不需要static修饰,但需要类名::的形式指定类域
int A::Count1 = 1;	
int A::Count2 = 2;
int main()
{
	A aa;
	aa.abc1();
	//public修饰的成员变量可以在类外访问
	cout << A::Count1 << endl;
	cout << aa.Count1 << endl;
	aa.abc2();
	//private修饰的成员变量不能再类外访问
	//cout << A::Count2 << endl;
	//cout << aa.Count2 << endl;
	return 0;
}
static member function has no hidden this pointer< /span> , cannot access any non-static members

Static member function cannot call non-static member function becausedoes not have this pointer

Non-static member functions can call static member functions of the class

class A {
public:
	static int abc1()
	{
		//A::public静态成员变量可以在类内表示
		return Count1;
	}
	static int abc2()
	{
		//A::private静态成员变量可以在类内表示
		return Count2;
	}
	//静态成员变量在类中声明
public:
	static int Count1;
private:
	static int Count2;
};
//静态成员变量在类外定义并且初始化,
//不需要static修饰,但需要类名::的形式指定类域
int A::Count1 = 1;
int A::Count2 = 2;
int main()
{
	A aa;
	//静态成员函数调用时,突破类域就可以
	cout << A::abc1() << endl;
	cout << aa.abc1() << endl;
	return 0;
}

1.3 Summary

Static member functions and static member variables are essentially restricted global variables and global functions, exclusive to this class, and subject to access qualifiers and class domain restrictions.

2.explicit

2.1 Implicit type conversion

class Date
{
public:
	Date(int year = 2023, int month = 1, int day = 1)
	{
		_year = year;
		_month = month;
		_day = day;
	}
private:
	int _year;
	int _month;
	int _day;
};
int main()
{
	Date a1(2023,11,13);
	Date a2;
	Date a3;
	//相当于2024这个整形隐式类型转换为了Date形
	//用2024构造一个临时变量,类型为const Date,临时变量再去赋值构造a2;
	a2 = 2024;
	//c++11规定可以传多个参数
	a3 = { 2024,3,3 };
	return 0;
}

It should be noted that the temporary variable of implicit type conversion is of const type, and permission issues need to be considered.

Permissions can be panned and reduced, but cannot be enlarged! ! !

const can be passed to const (permission translation), and const can be passed to non-const (permission Zoom out)

Non-const member function  cannot  be passed to non-const (Permission amplification), non-const can be passed to const (Permission translation)

2.2The role of expilcit

Modifying the constructor with explicit will disable implicit conversion of the constructor .
But if it is a display type conversion, expilcit cannot be disabled.

3. Tomomoto

3.1 Friend functions

Friend functions can directly access classes’ private member, which is a ordinary function defined outside the class > keywordfriend, does not belong to any class, but needs to be declared inside the class. When declaring, you need to add the

When we need to access private or protected members of an object in a global function, we need to declare a friend function in the class

class Date
{
	//声明友元函数,注意是声明,不是定义!!!
	friend int add(Date aa);
public:
private:
	int _year;
	int _month;
	int _day;
};
int add(Date aa)
{
	//在全局函数中需要访问一个类的私有时,需要在类中声明友元函数
	return aa._year + aa._month;
}
Friend functions can access private and protected members of the class, but are not member functions of the class
Friend function for impossible const modification
Friend functions can be declared anywhere in the class definition, are not restricted by class access qualifiers
A function can be a friend function of multiple classes
The principle of calling friend functions is the same as that of ordinary functions.

3.2 Friend class

All member functions of a friend class can be friend functions of another class and can access non-public members of another class.
Friend relationships are one-way and non-commutative.

as a resultCisBtomomoto,< /span>'s friend, no explanationAis BtimeA's friend.

class Date1
{
	//声明Date2是Date1的友元类,
	//Date2可以访问Date1的私有或者保护成员
	friend class Date2;
private:
	int _year1;
	int _month1;
	int _day1;
};
class Date2
{
public:
	//可以访问Date1中的私有或者保护成员
	int add(Date1 d1)
	{
		return d1._year1;
	}
private:
	int _year2;
	int _month2;
	int _day2;
};

Guess you like

Origin blog.csdn.net/qq_73955920/article/details/134351857