under classes and objects

1. Initialization list

1. Grammar:

Initialization list:
Starts with a colon, followed by a comma-separated list of data members, each "member variable" followed by an initial value or expression enclosed in parentheses

  Each member variable can only appear once in the initialization list (initialization can only be initialized once). The class contains the following members, which must be placed in the initialization list for initialization:

1. Reference member variable
2. Const member variable
3. User-defined type member (and the class has no default constructor)

2. Initialization sequence

  member variable in classThe order of declaration is the order of initialization in the initialization list, regardless of its order in the initialization list

summary:

Advantages of the initialization list:
1. Improve program efficiency
Using the initialization list can avoid the assignment of member variables in the constructor, thereby improving the operating efficiency of the program.
2. Clarify the initialization order of member variables
When a class contains multiple member variables, their initialization order may affect the correctness of the program. If the assignment symbol is used for initialization, each variable must write its initialization statement separately, which is not intuitive and error-prone. Using the initialization list can clearly stipulate the initialization order of variables, reducing the probability of errors.
3. Support the initialization of constant member variables and reference type member variables
Inside the constructor body, constant member variables or reference type member variables cannot be initialized directly. But the initialization of member variables of these types can be realized by using the initialization list.

Two, static members

  Class members declared 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

Features:
1. Static members are shared by all class objects, do not belong to a specific object, and are stored in the static area.
2. Static member variables must be defined outside the class. The static keyword is not added when defining, and only declared in the class.
3. , Class static members can be accessed by class name:: static members or object. static members
4. Static member functions have no hidden this pointer, and cannot access any non-static members
5. Static members are also members of classes, subject to public, Restrictions for protected, private access qualifiers

3. Friends

1. Friend function

Syntax: friend + friend +f r i e n d + function declaration

A friend function can directly access the private members of a class. It is an ordinary function defined outside the class and does not belong to any class, but it needs to be declared inside the class. The keyword friend needs to be added when declaring

Note:
1. Friend functions can access private and protected members of the class, but not member functions of the class
2. Friend functions cannot be modified with const
3. Friend functions can be declared anywhere in the class definition and are not limited by class access 4. A function
can be a friend function of multiple classes
5. The principle of calling a friend function is the same as that of a normal function

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. For example, Time class and Date class, inThe Date class is declared as a friend class in the Time class, then you can directly access the private member variables of the Time class in the Date class, but you cannot access the private member variables of the Date class in the Time class

Note:
The friendship relationship is one-way and not exchangeable.
The friendship relationship cannot be transmitted

  If a class is defined inside another class, the inner class is called an inner class. The inner class is an independent class, it does not belong to the outer class, let alone access the members of the inner class through the object of the outer class. ==Inner classes are born as friend classes of outer classes,==Outer classes do not have any superior access rights to inner classes, see the definition of friend classes, inner classes can access the outer class through the object parameters of the outer class all members. But the outer class is not a friend of the inner class

Features: The inner class can be defined in the public, protected, and private of the outer class.
2. Note that the inner class can directly access the static members of the outer class without the object/class name of the outer class.
3. sizeof (outer class) = outer class, has nothing to do with inner class

4. Compiler optimization when copying objects

For the following class, let's take a look at what optimizations the compiler makes when copying objects

class A
{
    
    
public:
	A(int a = 0)
		:_a(a)
	{
    
    
		cout << "用数字初始化" << endl;
	}

	A(const A& aa)
		:_a(aa._a)
	{
    
    
		cout << "用对象初始化" << endl;
	}

	A& operator=(const A& aa)
	{
    
    
		cout << "用一个对象赋值" << endl;
		if (this != &aa)
		{
    
    
			_a = aa._a;
		}
		return *this;
	}
private:
	int _a;
};

Example 1,


Example 2,


example 3,


Summary:
  The article is very short, and there are not many knowledge points under classes and objects, so this is the end. Looking forward to our next blog meet

Supongo que te gusta

Origin blog.csdn.net/Front123456/article/details/130665089
Recomendado
Clasificación