日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程语言 > c/c++ >内容正文

c/c++

C++11 explicit关键字的作用

發(fā)布時(shí)間:2023/12/13 c/c++ 40 豆豆
生活随笔 收集整理的這篇文章主要介紹了 C++11 explicit关键字的作用 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

explicit

  • 在C++中,explicit關(guān)鍵字用來(lái)修飾類(lèi)的構(gòu)造函數(shù),被修飾的構(gòu)造函數(shù)的類(lèi),不能發(fā)生相應(yīng)的隱式類(lèi)型轉(zhuǎn)換,只能以顯示的方式進(jìn)行類(lèi)型轉(zhuǎn)換。因?yàn)闊o(wú)參構(gòu)造函數(shù)和多參構(gòu)造函數(shù)本身就是顯示調(diào)用的。再加上explicit關(guān)鍵字也沒(méi)有什么意義。

explicit使用注意事項(xiàng):

  • explicit 關(guān)鍵字只能用于類(lèi)內(nèi)部的構(gòu)造函數(shù)聲明上
  • explicit 關(guān)鍵字作用于單個(gè)參數(shù)的構(gòu)造函數(shù)。
  • 在C++中,explicit關(guān)鍵字用來(lái)修飾類(lèi)的構(gòu)造函數(shù),被修飾的構(gòu)造函數(shù)的類(lèi),不能發(fā)生相應(yīng)的隱式類(lèi)型轉(zhuǎn)換
  • 禁止類(lèi)對(duì)象之間的隱式轉(zhuǎn)換

不使用explicit

#include <cstring> #include <string> #include <iostream> using namespace std; class Explicit{private:public:Explicit(int size){cout << "The size is " << size << endl;}Explicit(const char* str){string _str = str;cout << "The str is " << _str << endl;}Explicit(const Explicit& ins){cout << "The Explicit is ins " << endl;}Explicit(int a,int b){cout << "The a is " << a << ",the b is "<< b << endl;} };int main(){Explicit test0(15);Explicit test1 = 15;//隱式調(diào)用Explicit(int size)Explicit test2("QWERASDF");Explicit test3 = "QWERASDF";//隱式調(diào)用Explicit(const char* str)Explicit test4(1,10);Explicit test5 = test1; }
  • test0和test1是相同類(lèi)型的,前者是顯示調(diào)用,后者是隱式調(diào)用。
  • 同理,test2和test3也是一致的
  • test4是調(diào)用加法運(yùn)算,test5調(diào)用的函數(shù)要是Explicit就返回?The Explicit is ins.

  • 上面的程序雖然沒(méi)有錯(cuò)誤,但是對(duì)于Explicit test1 = 10;和Explicit test2 = "BUGBUGBUG";這樣的句子,把一個(gè)int類(lèi)型或者const char*類(lèi)型的變量賦值給Explicit類(lèi)型的變量看起來(lái)總歸不是很好,并且當(dāng)程序很大的時(shí)候出錯(cuò)之后也不容易排查。所以為了禁止上面那種隱式轉(zhuǎn)換可能帶來(lái)的風(fēng)險(xiǎn),一般都把類(lèi)的單參構(gòu)造函數(shù)聲明的顯示調(diào)用的,就是在構(gòu)造函數(shù)加關(guān)鍵字``explicit`。如下:

#include <cstring> #include <string> #include <iostream> using namespace std; class Explicit{private:public:explicit Explicit(int size){cout << "The size is " << size << endl;}explicit Explicit(const char* str){string _str = str;cout << "The str is " << _str << endl;}Explicit(const Explicit& ins){cout << "The Explicit is ins " << endl;}Explicit(int a,int b){cout << "The a is " << a << ",the b is "<< b << endl;} };int main(){Explicit test0(15);//Explicit test1 = 15;//隱式調(diào)用Explicit(int size) 無(wú)法使用Explicit test2("QWERASDF");//Explicit test3 = "QWERASDF";//隱式調(diào)用Explicit(const char* str) 無(wú)法使用Explicit test4(1,10);Explicit test5 = test0; }

  • 上面再寫(xiě)Explicit test1=10; Explicit test3 = "BUGBUGBUG";這樣的句子的時(shí)候程序就會(huì)報(bào)如下錯(cuò)誤:

error: conversion from ‘int’ to non-scalar type ‘Explicit’ requested
error: conversion from ‘const char [10]’ to non-scalar type ‘Explicit’ requested

參考鏈接

  • https://www.jianshu.com/p/af8034ec0e7a

?

創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)

總結(jié)

以上是生活随笔為你收集整理的C++11 explicit关键字的作用的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。