C++: Namespace, default functions, and function overloading


1. Namespace

1. Why is there a namespace?

In large programs, multiple independently developed libraries are often used. These libraries often define a large number of global names, such as classes, functions, and templates. When an application uses multiple libraries, it is inevitable that certain names will conflict with each other. Putting multiple library names in global variables will cause namespace pollution.
Namespace provides a more controllable mechanism for preventing name conflicts. The namespace divides the global namespace, and each namespace will be a scope.

2. The definition method of namespace

A namespace consists of two parts: first is the keyword namespace, followed by the name of the namespace. Following the namespace name is a series of declarations and definitions enclosed in curly braces.
The following briefly introduces the definition method of namespace:

//普通的命名空间
namespace N1
{
    
    
    int a;
    int Fun(int a,int b)
    {
    
    
        return a + b;
    }
}

Note: There is no need to add a semicolon after the namespace scope.

//命名空间可以嵌套
namespace N2
{
    
    
    int a;
    int Fun(int a,int b = 0)
    {
    
    
        return a + b;
    }
    namespace N3
    {
    
    
        int b;
        int Fun(double a,int b)
        {
    
    
            return a + b;
        }
    }
}
//在同一个工程中允许存在多个相同的命名空间,编译器最后会合成同一个命名空间中
namespace N1{
    
    
    int Fun(int a,int b)
    {
    
    
        return a + b;
    }
}

namespace N2{
    
    
    int Fun(int a = 1,int b = 2)
    {
    
    
        return a + b;
    }
}

namespace N1{
    
    
    int Fun(double a, double b)
    {
    
    
        return a - b;
    }
}

Note: A namespace defines a new scope, and all contents of the namespace are limited to that namespace.

3. Use of Namespace

//加命名空间名称及作用域限定符
namespace N1
{
    
    
    int a = 10,b = 20;
    int Add(int left,int right)
    {
    
    
        return left + right;
    }
    int Sub(int left,int right)
    {
    
    
        return left - right;
    }
}
int main()
{
    
    
    printf("%d\n",N1 :: a);
    return 0;
}
//使用using将命名空间中的成员引入
namespace N1
{
    
    
    int a = 10,b = 20;
    int Add(int left,int right)
    {
    
    
        return left + right;
    }
    int Sub(int left,int right)
    {
    
    
        return left - right;
    }
}
using N1 :: b;
int main()
{
    
    
    printf("%d\n",N1 :: a);
    printf("%d\n",b);
    return 0;
}
//使用using namespace 命名空间名称引入
namespace N1
{
    
    
    int a = 10,b = 20;
    int Add(int left,int right)
    {
    
    
        return left + right;
    }
    int Sub(int left,int right)
    {
    
    
        return left - right;
    }
}
using namespace N1;
int main()
{
    
    
    printf("%d\n",N1 :: a);
    printf("%d\n",b);
    printf("%d\n",Add(10,20));
    return 0;
}

Two, the default parameters

1. What are the default parameters?

The default parameter is to specify a default value for the function parameter when the function is declared or defined. When the function is called, if there is no specified actual parameter, the default value is used, otherwise the specified actual parameter is used

2. Classification of default parameters

using namespace std;
//全缺省参数
void Fun1(int a,int b)
{
    
    
    std::cout << "a = " << a << endl;
    std::cout << "b = " << b << endl;
}

//半缺省参数
void Fun2 (int a,int b = 20)
{
    
    
    std::cout << " a = " << a << endl;
    std::cout << " b = " << b << endl;
}

Note:1. The semi-default parameters must be given once from right to left, and cannot be assigned randomly.
2. The default parameters cannot appear in the function declaration and definition at the same time.
3. The default value must be a constant.
4. C language does not support default parameters (the compiler does not support).

Three, function overloading

1. What is function overloading?

Function overloading is a special case of functions. C++ allows several functions of the same name with similar functions to be declared in the same scope. The formal parameter lists (number or type or order of parameters) of these functions of the same name must be different. They are usually used Deal with problems with similar functions but different data.
Here are a few examples:

using namespace std;
int Add(int a,int b)
{
    
    
    return a + b;
}

double Add(double a, double b)
{
    
    
    return a + b;
}

float Add(float a, float b)
{
    
    
    return a + b;
}

int main()
{
    
    
    Add(10,20);
    Add(10.0,20.0);
    Add(0.1f,0.3f);
    return 0;
}

2. Why does C++ support function overloading, but C language does not support overloading?

In C/C++, a program needs to go through four stages to run: preprocessing, compilation, assembly, and linking. The following is the result of compiling two programs of C and C++ under linux:
C++:
Insert picture description here

With
It can be seen from the above two figures that after compiling with g++, the modification of the function name has changed. The compiler adds the function parameter type information to the modified name.

C language
Insert picture description here

Insert picture description here
Under Linux, after compiling with gcc, the modification of the function name has not changed.

In summary, C++ can be seen during the compilation process that function naming 3 depends on the parameters of the function (including the number or type or order of the parameters), and the C language treats the same in the compilation process.

Why can't a function be overloaded when the return value of the same type is different?

Make an assumption: Function overloading is related to return value types and formal parameter types. For
example:
Insert picture description here

We can observe that int Add is rewritten to _Z3Addii during the compilation stage , and double Add is rewritten to **_Z3Addii**, so guess I ->int d ->double, the number 3 after Z represents the return type (this should be the same as Compiler related)

So the reason why the return type of the function is not considered is because the parsing operator or function call is independent of the upper and lower statements. If the return value type is taken into account in the function overloading, the compiler cannot determine which function to call.

Interview questions

There are a few simple
interview questions, to list them out: 1. Why can't function overloading be supported in C language?
2. How is the bottom layer overloaded in C++ handled?
3. Can a function be written in the style of C language in C++?

Guess you like

Origin blog.csdn.net/xhuyang111/article/details/114362768