Namespace, default parameters, function overloading, reference


 
 
 

1. Namespace:

A namespace defines a new scope , and all content within it is limited to that namespace.

The purpose of this: to avoid naming conflicts or name pollution .

How the namespace is defined :

  1. Common:
namespace N
{
	int a;
	int ADD(int left, int right)
	{
		return left + right;
	}
}
  1. Nested definition:
namespace N
{
	int a;
	int ADD(int left, int right)
	{
		return left + right;
	}
	namespace N1
	{
		int a;
		int ADD(int left, int right)
		{
			return left + right;
		}
	}
}
  1. In the same file, there are multiple namespaces with the same name, and the compiler will eventually merge into one namespace
namespace N
{
	int a;
	int ADD(int left, int right)
	{
		return left + right;
	}
}
namespace N
{
	int c;
	int d;
}

Note: The third method is the same namespace, written in different locations, and will eventually be merged, and there cannot be the same variable or function name (the parameter types are exactly the same). Otherwise, redefinition will occur .

Use of namespace:

  1. Scoping operator ::using: , N::变量/函数
    such as:N::c;
  2. The use of using N(命名空间名):: 变量/函数
    certain variables or functions such cases for a namespace often used, can be directly used as such. In this way, the variable becomes a "global variable" in the current file, and the function can be called directly in the entire project.
    Such as:
using N::ADD;// ②
using N::a;

int main()
{
	N::c;//①
	return 0;
}
  1. Use using namespace N(命名空间);
    the namespace variable or function can be used quite frequently. All the member scopes in the namespace are regarded as the global role of the file.

 
 
 

2. Default parameters:

The default parameter is to specify a default value for the parameter when declaring or defining a function . When the function is called, if the actual parameter is not specified, the default value is used, otherwise the specified actual parameter is used .

Default parameter type:

  1. Full default parameters: All function parameters have default values.
int ADD(int first = 1, int second = 2, int third = 3)
{
	return first + second + third;
}
  1. Semi-default parameters: Only some function parameters have default values.
int ADD(int first, int second, int third = 3)
{
	return first + second + third;
}

note:

  1. The semi-default parameters must be given from right to left .
  2. Default parameters cannot appear in the function declaration and definition at the same time .
  3. The default value must be a constant or a global variable

It can be given in the function declaration and definition alone. If it appears repeatedly, redefine default parameters will appear .

It is generally recommended that the default parameter default values ​​be given in the function declaration.

3. Function overloading:

Function overloading : In C++, several functions of the same name with similar functions are declared in the same scope . The formal parameter lists (parameter types, number of parameters, parameter order) of these functions with the same name are different.

Different manifestations of the parameter list:

  1. The number of parameters is different: the following two functions with the same name, with different numbers of parameters, constitute a function overload.
int ADD(int right, int left)
{
	return right + left;
}

int ADD()
{
	return 10;
}
  1. Different parameter types : The following two functions with the same name have different parameter types, which constitute a function overload.
int ADD(int right, int left)
{
	return right + left;
}

double ADD(double right,double left)
{
	return right + left;
}
  1. The order of the parameter types is different : the order of the parameter types is different for the following two functions with the same name, which constitutes a function overload.
double ADD(int right, double left)
{
	return right + left;
}

double ADD(double right, int left)
{
	return right + left;
}

Such a code:

int ADD(int right, int left)
{
	return right + left;
}

double ADD(double right, double left)
{
	return right + left;
}

int main()
{
	ADD(1, 2.2);
	return 0;
}

What kind of errors will appear in this code?

In the process of function call, the formal parameters will be instantiated, but there is no complete matching type . Implicit type conversion will occur . int->double 或者double->intType, but in the above call, you will find that both conversions can be performed. The compiler does not know which one to choose. It will produce an error.

Error reporting in VS2017:
Insert picture description here
This error is called ambiguity | ambiguity.

Function overload calling principle: determine what function to call at the compilation stage

  1. In the compilation stage, the compiler deduces the type , and finds the function matching the type according to the deduction result to call.
  2. If there is a function with a completely matching type , call it directly.
  3. If there is no function whose type matches exactly , an implicit type conversion is performed .
  4. If there is a corresponding function ( without ambiguity ) after the implicit type conversion, it is called.
  5. If there is no or ambiguity, an error will be reported .

Therefore, when ADD (1, 2.2) is executed, ambiguity, multiple conversions, and compiler errors occur.

The return value type of the function is different and does not constitute a function overload.

int ADD(int right, int left)
{
	return right + left;
}

double ADD(int right, int left)
{
	return right + left;
}

This does not constitute function overloading.

Why doesn't C language support function overloading? And C++ supports function overloading?

Take a look in the VS environment:

int ADD(int right, int left);

int main()
{
	ADD(1, 2);
	return 0;
}

If there is no function definition, an error is reported.
In C++, the error is:
Insert picture description here
In C language, the error is: The
Insert picture description here
above code is the same, but the compiler uses different names at the bottom when compiling.

C language: just add a _
C++ to the function name: interpret the function name as ?ADD@@YAHHH@Z.
If in C++, the return value type is double, the parameter type is double, what is the function name?
Insert picture description here
Insert picture description here

 
 
 
Let's take a look at how it is in the Linux environment:
C++:
Insert picture description here
C language:
Insert picture description here
You can see that in C language, the modification of functions is relatively simple. C++ is more specific to function decoration. In Linux, it includes the length of the function name and the first letter of the function parameter type.

Therefore, C language does not support function overloading, because it modifies functions relatively simply. If there are functions with the same name, redefinition will occur. In C++, function parameter types are also modified. Different parameter lists of functions with the same name have different underlying descriptions.

 
 
 

What is the difference between C language and C++ function?

  1. Return value type:

In C language: the function has no return value type, and the system returns int type by default.
C++: Need to bring the return value type. Otherwise, the program reports an error.

  1. Function parameters:

In C language: the function has no parameters, but when calling, the parameters are passed, and the compiler can compile it.
In C++, the function has no parameters. When calling, the parameters are passed, and the compiler cannot compile it.

  1. C++ function parameters can have default values: the default parameters
  2. Function naming rules are different: C++ supports function overloading, but C language does not support

The C++ compiler has stricter syntax detection than the C language compiler.
 
 
 
 

4. Quote:

Reference is an alias for the current existing variable . The compiler will not open up space for the reference , but shares a memory space with the referenced variable.
Insert picture description here
Reference and referenced variables share the same memory space.
If the data is changed, the referenced variable will also change.

The reference type must be consistent with the variable type.

Note quoted:

  1. Must be initialized when defining . That is, specify the variable to be referenced.
  2. A variable can have multiple references. That is, an entity has multiple aliases.
  3. After referencing an entity, no other entities can be referred to.

When referencing a constant, const must also be added, and the entity cannot be modified by reference.

int main()
{
	double d = 12.56;
	const int& rd = d;
	return 0;
}

The above code can be quoted successfully. But there will be problems:
Insert picture description here

Why is this?
The reference variable will be consistent with the type of the reference entity. There is an implicit type conversion between double and int, and double is converted to int.
rd refers to d, the compiler creates a temporary space as a transition, let rd to refer to this temporary space.
But the address of this space is not known under normal circumstances and cannot be modified. So this space has constancy (constant characteristics).
rd is equivalent to quoting a constant. You must add a const.

 
 
Referenced application scenarios:

  1. Keep the code concise.
  2. Make function parameters.
  3. Do function return value.

Pay attention to the function return value:

  1. If the returned object has been returned to the system, it cannot be returned by reference. Otherwise, an illegal area is returned.
  2. If the returned object is not returned to the system, return by reference can be used.

Comparison of function passing by value and passing by reference: Passing by reference is much faster than passing by value, because there is no process of instantiation of formal parameters by reference, and formal parameters are just an alias of the entity, which reduces the overhead caused by parameter stacking, so Much faster.

Comparison of function passing by pointer and passing by reference: the efficiency of the two is similar.

void Swap(int& right, int& left)
{
	int tmp = right;
	right = left;
	left = tmp;
}

void Swap(int *right, int *left)
{
	int tmp = *right;
	*right = *left;
	*left = tmp;
}

int main()
{
	int a = 10;
	int b = 20;
	Swap(a, b);

	Swap(&a, &b);
	return 0;
}

Disassemble it:
Insert picture description here
found that the operations performed by the two codes are the same. References are operated in the manner of pointers.

It is implemented by means of reference operations and pointers at the bottom.

The difference between reference and pointer:

  1. From a conceptual point of view:
    Reference: is an alias of a variable, the compiler will not open up space for it. References and entities share a space.
    Pointer: The compiler will allocate space for it, and the pointer points to the address.
  2. From the bottom level: there is no difference between references and pointers-references are pointers
  3. From the characteristic point of view:

1. The reference must be initialized, and the pointer is not required
. 2. After the entity is referenced, other entities cannot be referenced. The pointer can point to any address that agrees with the type.
3. There is no NULL reference, but there is a NULL pointer.
4. In sizeof, the result of the reference is the size of the reference type, and the pointer is always 4 bytes on a 32-bit platform.
5. Referenced from Add 1 to the entity plus 1, and the pointer adds 1 to the size of its type.
6. There are multi-level pointers, but there is no multi-level reference.
7. When accessing the entity, the pointer needs to be dereferenced, and the reference can be used directly.
8. Reference ratio Pointers are relatively safer

Guess you like

Origin blog.csdn.net/w903414/article/details/108714494