📜  在 C++ 中使用显式关键字

📅  最后修改于: 2022-05-13 01:55:15.829000             🧑  作者: Mango

在 C++ 中使用显式关键字

C++ 中的显式关键字用于标记构造函数以不隐式转换 C++ 中的类型。对于只接受一个参数并在构造函数(带有单个参数)上工作的构造函数是可选的,因为它们是唯一可用于类型转换的构造函数。

让我们通过一个例子来理解显式关键字。

预测以下 C++ 程序的输出

CPP
// C++ program to illustrate default
// constructor without 'explicit'
// keyword
#include 
using namespace std;
 
class Complex {
private:
    double real;
    double imag;
 
public:
   
    // Default constructor
    Complex(double r = 0.0,
            double i = 0.0) : real(r),
                              imag(i)
    {
    }
 
    // A method to compare two
    // Complex numbers
    bool operator == (Complex rhs)
    {
        return (real == rhs.real &&
                imag == rhs.imag);
    }
};
 
// Driver Code
int main()
{
    // a Complex object
    Complex com1(3.0, 0.0);
 
    if (com1 == 3.0)
        cout << "Same";
    else
        cout << "Not Same";
    return 0;
}


CPP
// C++ program to illustrate
// default constructor with
// 'explicit' keyword
#include 
using namespace std;
 
class Complex {
private:
    double real;
    double imag;
 
public:
    // Default constructor
    explicit Complex(double r = 0.0,
                     double i = 0.0) :
                     real(r), imag(i)
    {
    }
 
    // A method to compare two
    // Complex numbers
    bool operator == (Complex rhs)
    {
        return (real == rhs.real &&
                imag == rhs.imag);
    }
};
 
// Driver Code
int main()
{
    // a Complex object
    Complex com1(3.0, 0.0);
 
    if (com1 == 3.0)
        cout << "Same";
    else
        cout << "Not Same";
    return 0;
}


CPP
// C++ program to illustrate
// default constructor with
// 'explicit' keyword
#include 
using namespace std;
 
class Complex {
private:
    double real;
    double imag;
 
public:
   
    // Default constructor
    explicit Complex(double r = 0.0,
                     double i = 0.0):
                     real(r) , imag(i)
    {
    }
 
    // A method to compare two
    // Complex numbers
    bool operator == (Complex rhs)
    {
        return (real == rhs.real &&
                imag == rhs.imag);
    }
};
 
// Driver Code
int main()
{
    // a Complex object
    Complex com1(3.0, 0.0);
 
    if (com1 == (Complex)3.0)
        cout << "Same";
    else
        cout << "Not Same";
    return 0;
}


输出
Same

正如本文所讨论的,在 C++ 中,如果一个类有一个可以用单个参数调用的构造函数,那么这个构造函数就成为一个转换构造函数,因为这样的构造函数允许将单个参数转换为正在构造的类。
我们可以避免这种隐式转换,因为这些可能会导致意想不到的结果我们可以借助显式关键字使构造函数显式化。例如,如果我们尝试使用带有构造函数的显式关键字的以下程序,则会出现编译错误。

CPP

// C++ program to illustrate
// default constructor with
// 'explicit' keyword
#include 
using namespace std;
 
class Complex {
private:
    double real;
    double imag;
 
public:
    // Default constructor
    explicit Complex(double r = 0.0,
                     double i = 0.0) :
                     real(r), imag(i)
    {
    }
 
    // A method to compare two
    // Complex numbers
    bool operator == (Complex rhs)
    {
        return (real == rhs.real &&
                imag == rhs.imag);
    }
};
 
// Driver Code
int main()
{
    // a Complex object
    Complex com1(3.0, 0.0);
 
    if (com1 == 3.0)
        cout << "Same";
    else
        cout << "Not Same";
    return 0;
}

输出

Compiler Error : no match for 'operator==' in 'com1 == 3.0e+0'

我们仍然可以将 double 值类型转换为 Complex,但现在我们必须显式地对其进行类型转换。例如,以下程序可以正常工作。

CPP

// C++ program to illustrate
// default constructor with
// 'explicit' keyword
#include 
using namespace std;
 
class Complex {
private:
    double real;
    double imag;
 
public:
   
    // Default constructor
    explicit Complex(double r = 0.0,
                     double i = 0.0):
                     real(r) , imag(i)
    {
    }
 
    // A method to compare two
    // Complex numbers
    bool operator == (Complex rhs)
    {
        return (real == rhs.real &&
                imag == rhs.imag);
    }
};
 
// Driver Code
int main()
{
    // a Complex object
    Complex com1(3.0, 0.0);
 
    if (com1 == (Complex)3.0)
        cout << "Same";
    else
        cout << "Not Same";
    return 0;
}
输出
Same