[C++ Essence Shop] 3. C++ entry reference (const), inline function

Table of contents

1. Quote

1.1 Citation properties

1.2 Frequently cited

1.2.1 Amplification of authority

 1.2.2 Reduced permissions

1.3 Usage Scenarios

1.3.1 Parameter passing 

1.3.2 Make return value

 1.4 Efficiency comparison between passing by value and passing by reference

 1.5 Differences between references and pointers

2. Inline functions

 2.1 inline

 2.2 Features


1. Quote

        In C++, a new concept reference is introduced, which is different from the traditional definition variable. The traditional definition variable is to open a new space to store data, while the reference is to give an alias to an existing space, which is the same as the reference variables share the same space.

1.1 Citation properties

  • References must be initialized at definition time
  • A variable can have multiple references
  • Once a reference refers to an entity, it cannot refer to another entity
	int a = 0;
	int& b = a;
	int& c = a;
	cout << a << ' ' << b << ' ' << c << endl;
	cout << &a << ' ' << &b << ' ' << &c << endl;

 output:

0 0 0
000000B0DDAFF6A4 000000B0DDAFF6A4 000000B0DDAFF6A4

        From the above output, it is obvious that the reference shares a space with the referenced entity.

1.2 Frequently cited

        Common references are references modified by the const keyword, but when we use common references, we will encounter various errors. In fact, it is the problem of enlarging and reducing permissions. The so-called permissions are whether they have the right to read or write. In C++, references support the reduction of permissions. However, it does not support privilege magnification.

1.2.1 Amplification of authority

        The first thing to explain is that C++ does not support privilege amplification. If privilege enlargement occurs, an error will be reported. (Privilege magnification is the increase of permissions. For example, the original variable can only be read and cannot be modified with const decoration. When you use a reference to refer to it without using const decoration, it can be modified. This is called permission magnification, which is not allowed by the compiler. )

	const int a = 0;
	//错误写法
	int& ra = a; //变量a有const修饰符,说明a只读不可写入,这里没有用const修饰,造成权限放大。
	//正确写法
	const int& rra = a;

        There is another situation where mistakes are particularly prone to occur, and people who make mistakes often feel that they have not experienced permission amplification, but it did happen and an error was reported. This is the scenario where type conversion occurs. For example, the following scene. 

 1.2.2 Reduced permissions

        In C++, permission reduction is allowed. For example, the original variable is readable and writable. You can use a const reference to refer to this entity, for example:

	int a = 0;
	const int& b = a;

1.3 Usage Scenarios

1.3.1 Parameter passing 

        A particularly typical application of parameter passing is swap, because the entity referenced by the reference shares the same space as the ontology, so a change in the reference is actually a change in the ontology. So swap exchanging variable values ​​is a very typical application.

void mySwap(int& a, int& b)
{
	swap(a, b);
}
int main()
{
	int a = 0, b = 1;
	cout << a << b << endl;
	swap(a, b);
	cout << a << b << endl;
}

 output:

01
10

1.3.2 Make return value

int& count()
{
	static int a = 0;
	a++;
	//...
	return a;
}

         But we should pay special attention to the scope and life cycle of the referenced object when returning the reference, so as not to cause extraordinary access, such as the following cases:

//错误案例
int& add(int a, int b)
{
	int c = a + b;
	return c;
}
int main()
{
	cout << add(1, 2) << endl;
}

        So here even though the compiler returns the correct result and doesn't even report an error, it's still a wrong use.

 1.4 Efficiency comparison between passing by value and passing by reference

        Values ​​are used as parameters or return types. During the period of passing parameters and returning, variables will not directly pass actual parameters or return the variable itself, but pass a temporary copy of the actual parameters or variables. If the type of copy that needs to be copied is particularly large Sometimes, the efficiency is extremely low. Passing by reference is to directly pass actual parameters or return the variable itself, and there is almost no consumption.    

 1.5 Differences between references and pointers

        In terms of grammatical concept, a reference is actually an alias without independent space, and shares the same space with the entity it refers to. But there is actually space in the underlying implementation, because references are implemented in the form of pointers. We can easily see it through the assembly code of references and pointers.

        Differences between references and pointers:

  1.  A reference conceptually defines an alias for a variable, and a pointer stores a variable address.
  2. References must be initialized when they are defined, pointers are not required.
  3. A reference can only refer to one entity and cannot be changed, and a pointer can point to any entity of the same type at any time.
  4. There are no NULL references, there are NULL pointers.
  5. sizeof(reference) is the size of the reference type, but the pointer is always the number of bytes occupied by the address space.
  6. The self-increment of the reference refers to the self-increment of the referenced entity, and the self-increment of the pointer means that the size of a type is offset backward.
  7. Pointers need to be explicitly dereferenced, references are handled by the compiler itself.
  8. References are safer than pointers.

2. Inline functions

 2.1 inline

        A function modified with the inline keyword is called an inline function. When compiling, C++ will be expanded at the place where the inline function is called. There is no overhead of building a stack frame for the function, which improves the efficiency of program operation. C++ expects to use inline to replace macros in C language (because macros are not convenient for debugging, they will reduce code readability, poor maintainability, and no type safety checks)

 2.2 Features

        Inline is a method of exchanging space for time. If the compiler treats the function as an inline function, it will replace the function call with the function body during the compilation phase, but it will increase the size of the object file.

        Inline is just a suggestion for the compiler. Different compilers may have different implementation mechanisms for inline. Generally, functions with small functions that are not recursive and frequently called are modified with inline, otherwise the compiler may ignore the inline feature.

        Inline also does not support the separation of declaration and definition, which will lead to link errors, because the inline function will be expanded without the function address. If the declaration and definition are separated, the address of the function cannot be found in the linking stage and an error will be reported.

Guess you like

Origin blog.csdn.net/qq_64293926/article/details/132131061