Notes on const and references in c++

1. Advantages of const string& over const string when passing parameters

The main reason is that const string& is more resource-saving than const string when passing parameters

How to pass by value and pass by reference in C++.
C++'s pass-by-value is to copy the value from the calling place to the function. There are two problems in doing this. First, the actual parameter needs to be copied to the formal parameter to form a copy of the actual parameter. , there is time and space overhead. If the actual parameter is a structure or class, then the time and space overhead will be very large; second, the modification of the copy of the actual parameter in the function will not affect the value of the actual parameter at the function call position.
C++ pass-by-reference, on the one hand, parameter passing is to copy the address of the actual parameter to the formal parameter, so that the formal parameter and the actual parameter correspond to the same memory address, then the modification of the formal parameter Naturally, it will be reflected in the actual parameters; on the other hand, pointers or references in C++ only occupy 4 bytes, so the space-time overhead is also acceptable.

The purpose of & is to reference, to avoid copying a std::string
const is to limit it to read-only

If it is const string s, it is still a waste to copy it again. Since it is already read-only, it can be used directly

Because C++ stipulates that the reference cannot be empty, you can use it directly when you pass in a reference from a function, and you need to judge whether the pointer is empty
if you pass in a value directly. For the object, you need to create a temporary object—this step The copy constructor needs to be called, and if the object itself is relatively large, it will cause a serious waste of resources. Passing references can avoid this-if we need to access an object read-only, using constant references can effectively avoid resource waste.

2. About const copy operation
Top-level const: the pointer itself is a constant
Bottom-level const: the object pointed to by the pointer is a constant

The general principle is that when copying an object, the top-level const of the object on the right side of the equal sign can be ignored, but the bottom-level const cannot, and the const on the left side of the equal sign has no effect. When it comes to what a pointer points to, or a reference, the same type is required, with two exceptions (non-constants can be assigned to constants, and derived class objects can be assigned to base class pointers or references).

1. Pointers are not involved. When referencing, the object itself is copied, and the original object will not be affected. All consts are the bottom layer, so all can be ignored (except that the consts are of the same type, or can be converted).

int a=0;
const int b=1;
a=b;
b=a;

insert image description here
Constant objects cannot call non-constant functions

Guess you like

Origin blog.csdn.net/qq_43050258/article/details/127279457