C++的explicit
时间: 2024-06-09 21:05:56 浏览: 17
在C++中,explicit关键字用于修饰只有一个参数的类构造函数,它的作用是表明该构造函数是显示的,而非隐式的。默认情况下,类构造函数被声明为implicit(隐式)。当一个构造函数被声明为explicit时,它将禁止编译器进行隐式类型转换。
在引用的代码中,displayPoint函数接受一个Point对象作为参数,但实际上却传递了一个int类型的值。由于Point类的构造函数没有被声明为explicit,编译器会进行隐式类型转换,将int类型的值转换为Point类型的对象。因此,1会被转换成一个临时的Point对象来调用displayPoint函数。
在引用中提到,explicit关键字只对有一个参数的类构造函数有效。当类构造函数参数大于或等于两个时,是不会发生隐式转换的,因此explicit关键字也就无效了。
那么,explicit关键字在类构造函数参数大于或等于两个时是否有效呢?实际上,无论有没有explicit关键字,类构造函数参数大于或等于两个时都不会发生隐式转换。
相关问题
c++ explicit
C++中的explicit关键字只能用于修饰只有一个参数的类构造函数,它的作用是表明该构造函数是显示的,而非隐式的。如果一个构造函数被explicit关键字修饰,则该构造函数不能在隐式转换中使用,只能在显式转换中使用。这样可以避免一些不必要的类型转换,提高代码的安全性和可读性。
以下是一个使用explicit关键字的例子:
```c++
class Test {
public:
explicit Test(int num) {
this->num = num;
}
private:
int num;
};
void func(Test t) {
cout << t.getNum() << endl;
}
int main() {
Test t1 = 10; // 编译错误,不能进行隐式转换
Test t2(20); // 正确,使用显式转换
func(30); // 编译错误,不能进行隐式转换
func(Test(40)); // 正确,使用显式转换
return 0;
}
```
C++ explicit
在C++中,explicit关键字主要用于修饰类的构造函数。被explicit修饰的构造函数的类,不能发生相应的隐式类型转换,只能以显示的方式进行类型转换。类构造函数默认情况下声明为隐式的,即implicit。\[3\] explicit关键字只能用于修饰只有一个参数的类构造函数,它的作用是表明该构造函数是显示的,而非隐式的。\[2\] 在给对象赋值时,如果构造函数被explicit修饰,则需要使用显示的方式进行类型转换,而不能使用隐式的方式。\[2\]
#### 引用[.reference_title]
- *1* [C++中explicit的用法](https://blog.csdn.net/qq_35524916/article/details/58178072)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item]
- *2* [C++中的explicit详解](https://blog.csdn.net/k6604125/article/details/126524992)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item]
- *3* [C++中的explicit](https://blog.csdn.net/ArthurHai521/article/details/123166526)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control,239^v3^insert_chatgpt"}} ] [.reference_item]
[ .reference_list ]
相关推荐
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)