c++ namespace and function overloading

1. Namespace

1. Why do naming conflicts occur?

  When we name a variable, we can use different methods, but beginners do not understand the named variables in the library, and there will be naming conflicts at this time

2. Two ways to resolve naming conflicts in C++

(1) Expand the namespace domain
  Expand the namespace domain to make local variables global variables

Syntax: using namespace + space domain that needs to be expanded

(2) Specify the access namespace domain

Syntax: (namespace field specified)::a

(3) Expand the specified namespace domain

Syntax: using std::(a certain namespace in the library function);

When a certain namespace domain is expanded, the following situations may occur. When both are global variables, the reference is unclear. Therefore, we only need to expand a certain part of the library function, and try to avoid naming conflicts caused by all expansions.

2. Function overloading

1. Concept

Function overloading: It is a special case of a function. C++ allows several functions of the same name with similar functions to be declared in the same scope. These functions with the same name have different formal parameter lists (parameter number or type or type order), which are often used to deal with Implementing functions is similar to the problem of different data types.

  In the C language, we often use add1(), add2(), such functions with different names, and C++ optimizes this. In short, only the function parameters need to be different, and there is no need to consider the return value type

2. Why does C++ support overloading?

  When the C compiler looks for a function definition, it only has the function name, not sure which function it is. When the C++ compiler finds the function definition, it also includes the parameters inside, ensuring the uniqueness of the definition

Three, the default function

1. Concept

A default function is a member function that provides default parameter values ​​in the class definition, and these parameters can be omitted when calling. If some parameters are omitted, the default values ​​in the definitions are used.

(1) Quanzhong Province

(2) Semi-default - can only default from right to left

2. Advantages

1. The code is concise and easy to read: the default function can reduce the length of the code, making the code more concise and easy to read.
2. Reduce repetitive code: defining a function with default parameters can avoid writing multiple similar overloaded functions, and only need to modify one function when modification is required.
3. Better scalability: without breaking existing code, you can easily add new parameters or modify default values ​​to achieve better scalability.
4. Convenient call: When using the default function, some unnecessary parameters can be omitted, so that the function call becomes more convenient.

4. Citation

1. Concept

A reference is not a new definition of a variable, but an alias for an existing variable. The compiler will not open up memory space for the reference variable, and it shares the same memory space with the variable it refers to.

  A piece of memory space is shared, and changing the alias also changes the original pointed content.

2. Advantages

1. Save memory space: A reference does not take up additional memory space like a pointer, it is just an alias of the target object, so it does not increase memory usage.
2. Simplify the code: References can simplify the writing of code, avoid frequent operations of dereferencing pointers, and make the code more concise and easy to read.
3. Safer: When using pointers, since pointers can point to any type of data, there are certain risks. The reference can only point to a specific type of data, so it is more secure and reliable.
4. Facilitate function parameter transfer: Function transfer references as parameters can realize the modification of original data without a large number of copy operations inside the function, thereby improving program efficiency.

  This blog is over here, recording C++ learning life bit by bit, looking forward to our next blog
!

Guess you like

Origin blog.csdn.net/Front123456/article/details/130182490