C++ primary grammar - type indicator auto and null pointer nullptr

Preface: The focus of this article is 1.
The type indicator auto in C++
2. The scope for implemented by auto
3. Why does C++ create nullptr instead of using the original NULL.

1.auto

1.The meaning and use of auto

In C++11, the standards committee gave the meaning of auto as: auto is no longer a storage type indicator, but serves as a new type indicator to instruct the compiler. Variables declared by auto must be compiled by the compiler. obtained by derivation.

#include<iostream>
using namespace std;

int main()
{
    
    
	int a = 10;
	auto b = 20;  
	auto c = 'x';
	auto d = 1.1;
	//typeid(变量名).name()能够显示变量的类型
	cout << typeid(a).name() << endl;    
	cout << typeid(b).name() << endl;
	cout << typeid(c).name() << endl;
	cout << typeid(d).name() << endl;//auto e;  错误示范,auto变量必须在定义时初始化
	return 0;
}

Here is the quote
Notice:Variables must be initialized when using auto to define them, because the compiler needs to deduce the actual type of auto based on the initialization expression during the compilation phase, so auto is not a "type" declaration, but a "placeholder" for type declaration. The compiler will use it during the compilation phase. Replace auto with the actual type of the variable.

2. Precautions for using auto

1. Variables must be initialized when defining them, as mentioned above.

2. When defining a row, the data must be of the same type.
Insert image description here

3.auto cannot be used as a parameter of a function.
This point is related to the first point, because the parameters are not initialized.
Insert image description here

4.auto cannot be used directly to declare arrays
Insert image description here

3. Scope for (highlight)

The for loop traversal in C language has a more convenient way in C++ - range for, which is very convenient to use.

int main()
{
    
    
	int arr1[] = {
    
     1,2,3 };
	//C语言写法
	for (int i = 0; i < sizeof(arr1) / sizeof(int); i++)
	{
    
    
		cout << arr1[i] << " ";
	}
	cout << endl;
	//C++写法
	//1.依次取arr1中数据赋值给x
	//2.自动判定结束
	//3.自动迭代(编译时编译器替换成迭代器)
	for (auto x: arr1)
	{
    
    
		cout << x << " ";
	}
	return 0;
}

Scope for Notes

Still taking the above code as an example, what do you think is displayed after the following code is run?

int main()
{
    
    
	int arr1[] = {
    
     1,2,3 };
	for (auto x: arr1)
	{
    
    
		x = x*2;
	}
	for (auto x : arr1)
	{
    
    
		cout << x << " ";
	}
	return 0;
}

The running results are as follows. You can see that there is no *2 in the data. This is because the above code only takes the data in arr1 and assigns it to x. The change of x will not affect the data in the array.
Here is the quote

So what should you do if you want to change?
Answer: YesUse references, adding & after auto means that x is an alias of the data in the array, so changing x will change the data in the array.
ButPointers cannot be used, because the pointer type does not match the original data type.
At this time, you can see the cleverness of C++'s use of references.

int main()
{
    
    
	int arr1[] = {
    
     1,2,3 };
	for (auto& x: arr1)  //auto后加上&,代表x是数组中数据的别名,这样改变x就会改变数组中的数据
	{
    
    
		x = x*2;
	}
	/*for (auto* x : arr1) //auto* 类型与arr1数据类型(int)不匹配
	{
		x = x * 2;
	}*/
	for (auto x : arr1)
	{
    
    
		cout << x << " ";
	}
	return 0;
}

2.nullptr

The previous articles mentioned that there are many loopholes in the C language, and nullptr was born to make up for the lack of NULL.
The following example builds the func function through function overloading. What do you think is displayed?
According to our original intention of designing the code, Func(0) should print int, and Func(NULL) should print int*.

void Func(int x)
{
    
    
	cout << "int" << endl;
}
void Func(int* x)
{
    
    
	cout << "int*" << endl;
}
int main()
{
    
    
	Func(0);
	Func(NULL);
	Func(nullptr);
}

The running results are as follows:
Insert image description here
It can be found that Func(NULL) calls the first function, which is different from the expected result.
This is because there is a macro definition in the C language #define NULL 0, NULL is defined as the literal constant 0.
Therefore, for the sake of code robustness, C++ later created nullptr.

BB at the end of the article: For friends who have any questions, feel free to leave a message in the comment area. If there are any questions, friends are welcome to point out in the comment area. The blogger will confirm the modification as soon as possible after seeing it. Finally, it is not easy to make. If it is helpful to friends, I hope to give the blogger some likes and attention.
Insert image description here

Guess you like

Origin blog.csdn.net/qq_73390155/article/details/132225688