显式关键字在C++中意味着什么?


当前回答

Cpp参考总是有用的!!!有关显式说明符的详细信息可以在此处找到。您可能需要查看隐式转换和复制初始化。

快速查看

显式说明符指定构造函数或转换函数(自C++11以来)不允许隐式转换或复制初始化。

示例如下:

struct A
{
    A(int) { }      // converting constructor
    A(int, int) { } // converting constructor (C++11)
    operator bool() const { return true; }
};

struct B
{
    explicit B(int) { }
    explicit B(int, int) { }
    explicit operator bool() const { return true; }
};

int main()
{
    A a1 = 1;      // OK: copy-initialization selects A::A(int)
    A a2(2);       // OK: direct-initialization selects A::A(int)
    A a3 {4, 5};   // OK: direct-list-initialization selects A::A(int, int)
    A a4 = {4, 5}; // OK: copy-list-initialization selects A::A(int, int)
    A a5 = (A)1;   // OK: explicit cast performs static_cast
    if (a1) cout << "true" << endl; // OK: A::operator bool()
    bool na1 = a1; // OK: copy-initialization selects A::operator bool()
    bool na2 = static_cast<bool>(a1); // OK: static_cast performs direct-initialization

//  B b1 = 1;      // error: copy-initialization does not consider B::B(int)
    B b2(2);       // OK: direct-initialization selects B::B(int)
    B b3 {4, 5};   // OK: direct-list-initialization selects B::B(int, int)
//  B b4 = {4, 5}; // error: copy-list-initialization does not consider B::B(int,int)
    B b5 = (B)1;   // OK: explicit cast performs static_cast
    if (b5) cout << "true" << endl; // OK: B::operator bool()
//  bool nb1 = b2; // error: copy-initialization does not consider B::operator bool()
    bool nb2 = static_cast<bool>(b2); // OK: static_cast performs direct-initialization
}

其他回答

其他答案缺少一个重要因素,我将在这里提及。

除了“delete”关键字,“explicit”允许您控制编译器生成特殊成员函数的方式——默认构造函数、复制构造函数、复制赋值运算符、析构函数、移动构造函数和移动赋值。

参考https://learn.microsoft.com/en-us/cpp/cpp/explicitly-defaulted-and-deleted-functions

构造函数附加隐式转换。若要抑制此隐式转换,需要使用显式参数声明构造函数。

在C++11中,您还可以使用这样的关键字指定“operator type()”http://en.cppreference.com/w/cpp/language/explicit使用这种规范,您可以在显式转换和直接初始化对象方面使用运算符。

P.S.当使用USER定义的转换(通过构造函数和类型转换运算符)时,只允许使用一级隐式转换。但您可以将这种转换与其他语言转换结合起来

向上整数列(char到int,float到double);标准转换(int到double);将对象指针转换为基类和void*;

假设您有一个类String:

class String {
public:
    String(int n); // allocate n bytes to the String object
    String(const char *p); // initializes object with char *p
};

现在,如果您尝试:

String mystring = 'x';

字符“x”将隐式转换为int,然后将调用String(int)构造函数。但是,这不是用户可能想要的。因此,为了防止出现这种情况,我们应明确定义构造函数:

class String {
public:
    explicit String (int n); //allocate n bytes
    String(const char *p); // initialize sobject with string p
};

显式关键字可用于强制显式调用构造函数。

class C {
public:
    explicit C() =default;
};

int main() {
    C c;
    return 0;
}

构造函数C()前面的显式关键字告诉编译器只允许显式调用此构造函数。

显式关键字也可以在用户定义的类型转换运算符中使用:

class C{
public:
    explicit inline operator bool() const {
        return true;
    }
};

int main() {
    C c;
    bool b = static_cast<bool>(c);
    return 0;
}

这里,显式关键字只强制显式强制转换为有效,因此bool b=c;在这种情况下将是无效的强制转换。在类似于这些显式关键字的情况下,可以帮助程序员避免隐式、非预期的强制转换。这种用法已在C++11中标准化。

Cpp参考总是有用的!!!有关显式说明符的详细信息可以在此处找到。您可能需要查看隐式转换和复制初始化。

快速查看

显式说明符指定构造函数或转换函数(自C++11以来)不允许隐式转换或复制初始化。

示例如下:

struct A
{
    A(int) { }      // converting constructor
    A(int, int) { } // converting constructor (C++11)
    operator bool() const { return true; }
};

struct B
{
    explicit B(int) { }
    explicit B(int, int) { }
    explicit operator bool() const { return true; }
};

int main()
{
    A a1 = 1;      // OK: copy-initialization selects A::A(int)
    A a2(2);       // OK: direct-initialization selects A::A(int)
    A a3 {4, 5};   // OK: direct-list-initialization selects A::A(int, int)
    A a4 = {4, 5}; // OK: copy-list-initialization selects A::A(int, int)
    A a5 = (A)1;   // OK: explicit cast performs static_cast
    if (a1) cout << "true" << endl; // OK: A::operator bool()
    bool na1 = a1; // OK: copy-initialization selects A::operator bool()
    bool na2 = static_cast<bool>(a1); // OK: static_cast performs direct-initialization

//  B b1 = 1;      // error: copy-initialization does not consider B::B(int)
    B b2(2);       // OK: direct-initialization selects B::B(int)
    B b3 {4, 5};   // OK: direct-list-initialization selects B::B(int, int)
//  B b4 = {4, 5}; // error: copy-list-initialization does not consider B::B(int,int)
    B b5 = (B)1;   // OK: explicit cast performs static_cast
    if (b5) cout << "true" << endl; // OK: B::operator bool()
//  bool nb1 = b2; // error: copy-initialization does not consider B::operator bool()
    bool nb2 = static_cast<bool>(b2); // OK: static_cast performs direct-initialization
}