c++ implicit call and explicit call

implicit call

Dynamic library and static library, the dynamic size is small, and the program needs to rely on the dynamic library file (.so file) when the program is running. Once the static library is compiled, it does not need to rely on the static library file (.a file)

Dynamic libraries are divided into explicit calls and implicit calls

implicit call explicit call
less calling code The name and function name added to the dynamic library when calling
Calling is as straightforward as using functions under the current project Load and unload by yourself (more reasonable, large projects use explicit)
The link command in the makefile needs to add the parameter -l command More flexible, the header file dlfcn.h must be added

Third-level directory

The constructor of est1 takes an int parameter, and line 23 of the code will be implicitly converted to call the constructor of Test1. The constructor of Test2 is declared as explicit (explicit), which means that this constructor cannot be called through implicit conversion, so a compilation error will occur on line 24 of the code. Ordinary constructors can be called implicitly. The explicit constructor can only be called explicitly.

class Test1
{
    
    
public:
    Test1(int n)
    {
    
    
        num=n;
    }//普通构造函数
private:
    int num;
};
class Test2
{
    
    
public:
    explicit Test2(int n)
    {
    
    
        num=n;
    }//explicit(显式)构造函数
private:
    int num;
};
int main()
{
    
    
    Test1 t1=12;//隐式调用其构造函数,成功
    Test2 t2=12;//编译错误,不能隐式调用其构造函数
    Test2 t2(12);//显式调用成功
    return 0;
}

Guess you like

Origin blog.csdn.net/weixin_41523437/article/details/120965568
Recommended