From C language to C++ advanced some C-based enhanced usage (part 5)

1. Enhanced detection of global variables

C++ has a stricter distinction between the detection and definition of global variables, which can be defined in C language global variables:

int a;  //定义
int a;  //声明
int a;  //声明

In the C language, the first one is regarded as a definition, and all subsequent variables with the same name are regarded as declarations.
In C++, this expression is wrong. Must use extern to modify:

int a;
extern int a;
extern int a;

2. New Bool type

That is, the Boolean type, which has only two values: true and false
. The use of the Boolean type in C language requires the #include <stdbool.h>introduction of a header file before the bool flag= true;bool type can be used.
In C++, there is no need to include a header file, and the bool type can be defined directly.

3. Enhancement of trinocular calculation

There are trinocular operations in C language, such as finding the maximum value:

int a=1, b=9;
big= a>b?a:b;

This means that if a>b, take a and assign it to big, otherwise, assign b to big. That is to say, in the C language, the ternary operation returns the value of the expression, which is a constant.

In C++, the ternary operation returns the result variable of the expression, which is a variable that can be re-assigned and operated.

#include <iostream>  //标准输入输出流
using namespace std;  //声明一段命名空间 ,且必须定义在全局范围内 

int main()
{
    
    
	int a=1, b=9;
	(a>b?a:b) = 999;
	cout << "a为:" << a << endl;
	cout << "b为:" << b << endl;
} 

Here, the ternary operator returns the variable with the maximum value, that is, b, and then assigns 999 to b, so the final output b is 999.

4. References to pointers and constants (alias)

(Pointer reference)
Formula type &p= q;for pointer reference: type is the pointer type.

#include <iostream>  //标准输入输出流
using namespace std;  //声明一段命名空间 ,且必须定义在全局范围内 

void fun(int* &p)
{
    
    
}
int main()
{
    
    
	int *p= NULL;
	fun(p);
}

(2) Constant reference The
alias of the constant is the reference to the constant. Simply put, adding a const before the reference is the reference const int &b= a;to the constant a, that is, the alias b.

5. Limitations of inline functions

In the following situations, the compiler may not compile Bingshu inline:

  • There can be any form of circulation
  • There cannot be too many conditional judgments
  • Function body cannot be too large
  • Cannot perform addressing operations on functions

6, the placeholder parameter of the function

To put it bluntly, it is a parameter that is used to occupy a position, and it may not be used in a function.

  • The placeholder parameter needs to specify the type, which means the placeholder;
  • When passing parameters, the value of the parameter must be passed in and cannot be omitted.
  • Placeholder parameters can specify default values ​​(default values)
#include <iostream>  //标准输入输出流
using namespace std;  //声明一段命名空间 ,且必须定义在全局范围内 

void fun(int a, int)
{
    
    
	cout << a << endl;
}
void fun2(int b, int=2)
{
    
    
	cout << b << endl;
}
int main()
{
    
    
	fun(99, 1);
	fun2(88);
}

7. The usage of extern "C"

This usage is used to declare the function in the .h external file, which means that it is a declaration of a C file, not a C++ file.
For example, the .c file is like this:
01
declare in the .h file that extern "C" should be included
02
so that we can call the mul function in the external file normally in the C++ file:

#include <iostream>  //标准输入输出流
using namespace std;  //声明一段命名空间 ,且必须定义在全局范围内 

//要调用外部文件需要先包含 头文件,也就是对ex.c文件进行声明的文件 
#include "ex.h"  
int main()
{
    
    
	cout << mul(3, 4) << endl;
}

Guess you like

Origin blog.csdn.net/Viewinfinitely/article/details/111935562