@[TOC]


1. This pointer

In the previous section of C++, when classes, objects, encapsulation, inheritance (derivation), polymorphic, this pointer appears in a member function, and we use the -> member extractor to manipulate the member variable.

In C++, every object can access its address through a pointer to this. This is an implicit argument to all member functions. In fact, by default the first argument to a member function is T* const register this. Clear after the execution of a member.

For example, let’s define a custom print function

class Animal
{
	String name;
	void print(a)
	{
		cout<<this->name; }}Copy the code

But from the compiler’s point of view, its prototype looks like this:

class Animal
{
	String name;
	void print(T* const register this)
	{
		cout<<this->name; }}Copy the code

Therefore, it can be used to point to the calling object inside a member function, and can only be called inside a member function. It cannot be used for global, static, or friend functions.

An object’s this pointer is not part of the object itself and does not affect the result of sizeof.


2. Static members

Adding the keyword static to a variable or function makes it a static variable or function. When static occurs in a member of a class that is being modified, it is called a static member.

I used to ask my teacher a very funny question, I will share with you, the code simplified is this:

class Animal
{
public:
	static int age;
}

Animal an[10];
for (int i = 0; i < 10; i++)
{
	aa[i].age = i;
}
Copy the code

When I output the age of these 10 objects, I am confused. All 10 ages are the same value, and the value is the last number. Why?

The static keyword sets the member variable age to a static variable.

Static local variables are initialized only once and prolong the life of the local variable until the end of the program.

In other words, within the body of a function, static variables have a “memory” function, that is, a declared static variable remains the same value for the duration of the function call. Each object has its own member variables, but they share static variables. So a static variable is essentially a common resource.

So this pointer cannot be used in static function, static functions like static variables, which he does not belong to a specific object, said the scope of the whole class static function in the sense of information, and this pointer is actually corresponds to an object, so use this pointer cannot be static function, in static function, Non-static members cannot be accessed directly.

Static data members are subject to the same public,protected, and private access rules as normal data members.

In contrast to members of a class, memory space is allocated only after the object is constructed and can only be accessed by the object name. Static members, on the other hand, can be accessed using the class name without constructing the object.

The difference between global variables and static variables:

Variables defined outside A function are called global variables (or external variables) whose scope is the entire project. For example, I defined A global variable in a.cpp: int A; So if I want to use this variable in b.pp, I can use the extern keyword in B.pp to use the variable A: extern A;

Static variables include static global variables and static local variables

In contrast to global variables, a static global is scoped in the CPP file in which it is defined. If there is only one CPP in a project, it is scoped in the same way as a global. If there are multiple CPPS, using extern in another CPP doesn’t help.

Static local variables only work in the function that defines them. In contrast to normal local variables, static local variables do not disappear when the function returns, whereas normal local variables do.

Static and global variables are set to 0 when uninitialized, while other variables are set randomly.


3. Friend functions friend classes

A friend function is a function declared in a class with the keyword friend. The function is declared in the class, but it is not a member function of the class. A friend function is not a member function of the class, but it can access members of the class, whether they are public, pirvate or protected.

For friend functions, it’s ok to put them in public and private, or protected.

class Animal
{
private:
	string name;
	friend void getName(a);
};

void getName(a)
{
	// Business logic
}
Copy the code

The reason for introducing friend functions is to reduce overhead and improve efficiency when implementing data sharing between classes. Specifically, in order to give member functions of other classes direct access to private variables of the class, while also breaking the class encapsulation, so you should not use friend functions if you have to.

Since you need friends to share data, in most cases, a friend function takes arguments. Since a friend function does not have a pointer to this, there are three cases of arguments:

To access a non-static member, you need an object as an argument.

To access static members or global variables, you do not need an object as an argument

If the object that takes a parameter is a global object, no object is required

The same is true for friend metaclasses.