Explicit: Explicit: Anonymous Spaces

Explicit keywords

Explicit is used to prevent constructor arguments from being automatically converted to class identifiers. It is not often used in code, but sometimes causes bugs that can’t be found.

Here’s an example to illustrate:

Case a

test.cpp

#include <iostream>

using namespace std;

class Test
{
public:
    Test(int a)
    {
        _num = a;
    }
    int getNum()
    {
        return _num;
    }
private:
    int _num;
};

int main()
{
    Test t = 'c';
    cout<<t.getNum()<<endl;
    return 0;
}
Copy the code

Let’s take a look at the results:

root@iZuf67on1pthsuih96udyfZ:~/C++/Net_C++/Lab_tt# g++ test.cpp -o test
root@iZuf67on1pthsuih96udyfZ:~/C++/Net_C++/Lab_tt# ./test 
99
Copy the code

We will find that procedure internally is automatically help we carried on the transformation, assuming that we would want is a character “c”, but was inexplicable change to 99, in fact, in some applications, we have not too concerned about the state of automatic conversion, but some open source code library, we read a lot of will to avoid this sort of thing, Using the keyword explicit is a weapon;

Let’s see what happens when the constructor uses explicit:

Case 2

#include <iostream>

using namespace std;

class Test
{
public:
    explicit Test(int a)
    {
        _num = a;
    }
    int getNum()
    {
        return _num;
    }
private:
    int _num;
};

int main()
{
    Test t = 'c';
    cout<<t.getNum()<<endl;
    return 0;
}
Copy the code

Let’s compile:

root@iZuf67on1pthsuih96udyfZ:~/C++/Net_C++/Lab_tt# g++ test.cpp -o test
test.cpp: In function'int main()' : test.cpp:22:14: error: conversion from 'char' to non-scalartype'Test' requested Test t ='c';
Copy the code

We will find that the explicit keyword is used to prevent the result from being automatically converted.

When writing a C++ class, we can omit this keyword if we can handle the type of the argument, but it is generally recommended to add this keyword, which can also increase the robustness of the code.

Anonymous namespace

For namespaces, I believe everyone is still quite familiar with, especially using namespace STD this paragraph, can be described as flowing! This one uses the STD namespace;

Static

We can look at static and recall: the data members used to modify a class are called “static members.” Such data members have a lifetime longer than class objects (instance /instance). Static data members have one copy per class, and ordinary data members have one copy per instance, so they are also called class variable and instance variable respectively. Unlike ordinary class data members, static class data members are independent of all class objects. Static class data members are associated with the class, but have no relationship to the objects defined by the class. Each static class object has a copy. All class objects share a static class member. For example, if A static member of class A is changed to 1, then the corresponding static member of class B is also changed to 1.

Anonymous Spaces are a new C++ alternative to global functions or variables that use static to define scopes for this compilation unit. Anonymous Spaces can be nested just like named namespaces. Extern = extern; extern = extern; extern = extern; extern = extern; extern = extern; extern = extern; extern = extern

—— source Internet

Let’s look at an example;

Case 3

#include <iostream>

using namespace std;

namespace 
{
    int num = 10;
}

namespace test
{
    namespace 
    {
        int num = 20;
    }
    void Print()
    {
        cout<<"_________________________"<<endl;
        cout<<"test::Print"<<endl;
        cout<<"test:num:"<<test::num<<endl;
        cout<<"num:"<<::num<<endl;
    }
}

int main()
{
    cout<<"_________________________"<<endl;
    cout<<"test::main"<<endl;
    cout<<"test:num:"<<test::num<<endl;
    cout<<"num:"<<::num<<endl;
    test::Print();
    return 0;
}
Copy the code

Results:

root@iZuf67on1pthsuih96udyfZ:~/C++/Net_C++/Lab_tt# ./test
_________________________
test::main
test:num:20
num:10
_________________________
test::Print
test:num:20
num:10
Copy the code

There are several advantages to using an anonymous space over static: 1. For a set of multiple identifier functions, only one anonymous space is required to declare static.

2. It can be nested. This allows you to use multiple identifiers with the same name in different namespaces.

To learn more about C++ background server, please pay attention to wechat official account: ====CPP background server development ====