【c++】【转】c++中的explicit关键字
http://www.cnblogs.com/chio/archive/2007/09/17/895263.html
c++中的explicit關(guān)鍵字用來修飾類的構(gòu)造函數(shù),表明該構(gòu)造函數(shù)是顯式(調(diào)用)的,既然有"顯式"那么必然就有"隱式",那么什么是顯示而什么又是隱式的呢?
如果c++類的構(gòu)造函數(shù)有一個(gè)參數(shù),那么在編譯的時(shí)候就會(huì)有一個(gè)缺省的轉(zhuǎn)換操作:將該構(gòu)造函數(shù)參數(shù)對(duì)應(yīng)數(shù)據(jù)類型的數(shù)據(jù)轉(zhuǎn)換為該類對(duì)象,如下面所示:
class MyClass { public:MyClass(int num){}; };MyClass obj = 10;//10被隱式調(diào)用構(gòu)造函數(shù)轉(zhuǎn)換為MyClass類型對(duì)象在上面的代碼中編譯器自動(dòng)將整型轉(zhuǎn)換為MyClass類對(duì)象,實(shí)際上等同于下面的操作:
MyClass temp(10);
MyClass obj = temp;
上面的所有的操作即是所謂的"隱式轉(zhuǎn)換"。
如果要避免這種自動(dòng)轉(zhuǎn)換的功能,我們?cè)撛趺醋瞿?#xff1f;這就是關(guān)鍵字explicit的作用了,將類的構(gòu)造函數(shù)聲明為"顯式",也就是在聲明構(gòu)造函數(shù)的時(shí)候前面添加上explicit即可,這樣就可以防止這種自動(dòng)的轉(zhuǎn)換操作,如果我們修改上面的MyClass類的構(gòu)造函數(shù)為顯式的,那么下面的代碼就不能夠編譯通過了,如下所示:
class MyClass { public:explicit MyClass(int num){}; };MyClass obj = 10;//出錯(cuò)?
?
class Rational { public:Rational(int numerator = 0, int denominator = 1){this->numerator = numerator;this->denominator = denominator;}~Rational(){}int get_numerator(){return numerator;}int get_denominator(){return denominator;}const Rational operator*(const Rational &rhs) const;private:int numerator;int denominator; };const Rational Rational::operator*(const Rational &rhs) const {return Rational(numerator * rhs.numerator, denominator*rhs.denominator); }int main() {Rational a(2, 1);//Rational b = 2 * a;//編譯出錯(cuò),相當(dāng)于2.operator*(a),而2不是Rational類型Rational b = a * 2;//2會(huì)被隱式類型轉(zhuǎn)換為Rational對(duì)象cout << b.get_numerator() << " " << b.get_denominator() << endl;system("pause");return 0; }上述有理數(shù)運(yùn)算不滿足交換律,所以需要改進(jìn)
class Rational { public:Rational(int numerator = 0, int denominator = 1){this->numerator = numerator;this->denominator = denominator;}~Rational(){}int get_numerator() const{return numerator;}int get_denominator() const{return denominator;}private:int numerator;int denominator; };const Rational operator*(const Rational& lhs,const Rational &rhs) {return Rational(lhs.get_numerator() * rhs.get_numerator(), lhs.get_denominator()*rhs.get_denominator()); }int main() {Rational a(2, 1);//2會(huì)被隱式類型轉(zhuǎn)換為Rational對(duì)象Rational b = 2 * a;//Rational b = a * 2;cout << b.get_numerator() << " " << b.get_denominator() << endl;system("pause");return 0; }總結(jié):如果你需要為某個(gè)函數(shù)的所有參數(shù)進(jìn)行類型轉(zhuǎn)換,那么這個(gè)函數(shù)必須是個(gè)non-member(effective c++ 條款24)
絕不要返回指針或引用指向一個(gè)local stack對(duì)象,因?yàn)閘ocal stack對(duì)象當(dāng)退出函數(shù)時(shí)就被析構(gòu)了,也就是說指針或者引用指向未知的內(nèi)存;不要返回引用指向一個(gè)heap-allocated對(duì)象,會(huì)導(dǎo)致內(nèi)存泄漏;不要返回指針或引用指向一個(gè)local static對(duì)象,因?yàn)橛锌赡芡瑫r(shí)需要多個(gè)這樣的對(duì)象,此時(shí)這些對(duì)象其實(shí)都是同一個(gè)對(duì)象。所以一個(gè)必須返回新對(duì)象的函數(shù)的正確寫法是:讓那個(gè)函數(shù)返回一個(gè)新對(duì)象,不用擔(dān)心拷貝構(gòu)造函數(shù)導(dǎo)致的性能損失。例子可見上面的operator*的寫法(effective c++ 條款21)
轉(zhuǎn)載于:https://www.cnblogs.com/ljygoodgoodstudydaydayup/p/3897349.html
總結(jié)
以上是生活随笔為你收集整理的【c++】【转】c++中的explicit关键字的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 设计根据标示符全局只运行一次的类
- 下一篇: CSS实现背景透明而背景上的文字图片不透