c++中的explicit关键字详解

在C++中,explicit 是一个关键字,用于修饰类的构造函数,以防止编译器进行隐式类型转换。它的主要作用是避免意外的隐式转换,从而提高代码的安全性和可读性。

1. 隐式类型转换的问题

在C++中,如果一个构造函数只接受一个参数(或者有多个参数但除了第一个参数外都有默认值),编译器可能会自动进行隐式类型转换。这种隐式转换有时会导致意外的行为。

class MyClass {
public:
    MyClass(int x) {
        std::cout << "Constructor called with value: " << x << std::endl;
    }
};

void printMyClass(const MyClass& obj) {
    std::cout << "Printing MyClass object" << std::endl;
}

int main() {
    MyClass obj = 10;  // 隐式转换:int -> MyClass
    printMyClass(20);  // 隐式转换:int -> MyClass
    return 0;
}

 输出:

Constructor called with value: 10
Constructor called with value: 20
Printing MyClass object

 

在上面的代码中:

1.MyClass obj = 10; 和 printMyClass(20); 都会触发隐式类型转换,将 int 转换为 MyClass 对象。

2.这种隐式转换可能会导致代码难以理解,甚至引发错误。

2. 使用 explicit 禁止隐式转换

通过在构造函数前加上 explicit 关键字,可以禁止编译器进行隐式类型转换。此时,必须显式地调用构造函数。

修改后的代码:
class MyClass {
public:
    explicit MyClass(int x) {
        std::cout << "Constructor called with value: " << x << std::endl;
    }
};

void printMyClass(const MyClass& obj) {
    std::cout << "Printing MyClass object" << std::endl;
}

int main() {
    // MyClass obj = 10;  // 错误:不能隐式转换
    MyClass obj(10);      // 正确:显式调用构造函数
    // printMyClass(20);  // 错误:不能隐式转换
    printMyClass(MyClass(20));  // 正确:显式调用构造函数
    return 0;
}

 输出:

Constructor called with value: 10
Constructor called with value: 20
Printing MyClass object

 使用 explicit 后,MyClass obj = 10; 和 printMyClass(20); 都会导致编译错误,必须显式地调用构造函数。

3. explicit 的使用场景

explicit 通常用于以下场景:

(1)单参数构造函数

如果一个构造函数只接受一个参数,通常应该将其声明为 explicit,以避免意外的隐式转换。

(2)多参数构造函数(C++11 及以后)

从 C++11 开始,explicit 也可以用于多参数构造函数,以防止隐式转换。

class MyClass {
public:
    explicit MyClass(int x, int y) {
        std::cout << "Constructor called with values: " << x << ", " << y << std::endl;
    }
};

int main() {
    // MyClass obj = {10, 20};  // 错误:不能隐式转换
    MyClass obj(10, 20);        // 正确:显式调用构造函数
    return 0;
}

 

4. explicit 的优点

1.避免意外的隐式转换:防止编译器自动进行不期望的类型转换,减少潜在的错误。

2.提高代码可读性:显式调用构造函数使代码的意图更加清晰。

3.增强类型安全:强制显式转换可以减少类型相关的错误。


5. 注意事项

1.explicit 只能用于构造函数。

2.如果确实需要隐式转换,则不应使用 explicit

3.在 C++11 及以后的标准中,explicit 还可以用于转换运算符(operator),以防止隐式类型转换。

示例:explicit 转换运算符
class MyClass {
public:
    explicit operator int() const {
        return 42;
    }
};

int main() {
    MyClass obj;
    // int x = obj;  // 错误:不能隐式转换
    int x = static_cast<int>(obj);  // 正确:显式转换
    return 0;
}

6. 总结

1.explicit 用于禁止构造函数的隐式类型转换。

2.它提高了代码的安全性和可读性,避免了意外的隐式转换。

3.在单参数构造函数和多参数构造函数中都可以使用 explicit

4.从 C++11 开始,explicit 还可以用于转换运算符。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值