天天看點

C++中的 explicit 關鍵字

C++中定義的 explicit  常在構造函數中使用防止C++的隐式轉換,可以阻止不應該允許的經過轉換構造函數進行的隐式轉換的發生。聲明為explicit的構造函數不能在隐式轉換中使用。

錯誤預防技巧11.3:

對于那些不應該被編譯器用來執行隐式轉換的單參數構造函數,請使用關鍵字explicit

explicit  關鍵字在函數聲明處使用:

示例:

  explicit Array(int = 10);

C++中, 一個參數構造函數

(或者除了第一個參數外其餘參數都有預設值的多參構造函數)

, 承擔了兩個角色。

1 一個參數的構造函數 是個構造器

2 一個參數的構造函數 是個預設且隐含的類型轉換操作符。

  任何單參數的構造函數都可以看做是轉換構造函數。

C++中的 explicit 關鍵字

C++中, 一個參數的 構造函數(或者除了第一個參數外其餘參數都有預設值的多參構造函數), 承擔了兩個角色。 1 是個 構造器 ,2 是個預設且隐含的類型轉換操作符。 是以, 有時候在我們寫下如 AAA = XXX, 這樣的代碼, 且恰好XXX的類型正好是AAA單參數構造器的參數類型, 這時候 編譯器就自動調用這個構造器, 建立一個AAA的對象。 這樣看起來好象很酷, 很友善。 但在某些情況下(見下面權威的例子), 卻違背了我們(程式員)的本意。 這時候就要在這個構造器前面加上explicit修飾, 指定這個構造器隻能被明确的調用,使用, 不能作為類型轉換操作符被隐含的使用。 呵呵, 看來還是光明正大些比較好。

示例 一: Array.h

#ifndef ARRAY_H
#define ARRAY_H

#include <iostream>
#include <iomanip>
using namespace std;






class Array{
	friend ostream& operator<< (ostream&, const Array &);
	friend istream& operator>>  (istream&, Array &);
public:
	Array(int arraySize = 10){
		size = (arraySize > 0 ? arraySize : 10);
		ptr = new int[size];
		for (int i = 0; i < size; i++)
			ptr[i] = 0;

	}
	Array(const Array  &arrayToCopy){
		ptr = new int[size];

		for (int i = 0; i < size; i++){
			ptr[i] = arrayToCopy[i];
		}
	}



	~Array(){
		delete[] ptr;
	}

	int getSize()const{
		return size;
	}

	const Array& operator=(const Array &right){
		if (&right != this){
			if (size != right.size){
				delete[] ptr;
				size = right.size;
				ptr = new int[size];
			}
			for (int i = 0; i < size; i++)
				ptr[i] = right.ptr[i];
		}
		return *this;
	}

	bool operator==(const Array &right)const{
		if (size != right.size)
			return false;

		for (int i = 0; i < size; i++)
			if (ptr[i] != right.ptr[i])
				return false;

		return true;
	}

	bool operator!=(const Array &right)const{
		return !(*this == right);
	}


	int operator[](int subscript)
	{
		if (subscript < 0 || subscript >= size){
			cerr << "\nError: Subscript " << subscript
				<< "out of range" << endl;
			exit(1);
		}

		return ptr[subscript];
	}
	int operator[](int subscript)const{
		if (subscript < 0 || subscript >= size)
		{
			cerr << "\nError: Subscript" << subscript
				<< "out of range" << endl;
			exit(1);
		}
		return ptr[subscript];
	}

private:
	int size;
	int *ptr;
};



#endif





istream& operator>>(istream &input, Array &a){
	for (int i = 0; i < a.size; i++)
		input >> a.ptr[i];
	return input;
}

ostream& operator<<(ostream &output, const Array &a){
	int i;

	for (i = 0; i < a.size; i++){
		output << setw(12) << a.ptr[i];

		if ((i + 1) % 4 == 0)
			output << endl;
	}

	if (i % 4 != 0)
		output << endl;

	return output;
}
           

主函數:

#include "Array.h"

void outputArray(const Array &);

int main()
{
	Array integers1(7);
	outputArray(integers1);
	outputArray(3);

	return 0;
}


void outputArray(const Array &arrayToOutput)
{
	cout << "The Array received has " << arrayToOutput.getSize()
		<< " elements. The contents are:\n" << arrayToOutput << endl;
}
           

輸出:

C++中的 explicit 關鍵字

可以看出我們并沒有定義

參數為int類型的outputArray的輸出函數。但是還是輸出了,這是什麼原因???
           
</pre><pre name="code" class="cpp" style="color: rgb(51, 51, 51); line-height: 23.9999980926514px; text-indent: 28px;">
           
<span style="line-height: 23.9999980926514px; font-family: arial, 宋體, sans-serif; font-size: 12px;">原因如下:</span><span style="line-height: 23.9999980926514px; font-family: arial, 宋體, sans-serif; font-size: 12px;">編譯器确定Array類是否提供了能把int轉換成Array的轉換構造函數。由于任何單參數的構造函數都可以看做是轉換構造函數,編譯器就認為接收單個int參數的Array構造函數是一個轉換構造函數,并用它将參數3轉換成了一個包含了三個元素的臨時Array對象。</span></strong></span></span>
           

解決方法: 修改Array.h代碼:

explicit Array(int arraySize = 10){
		size = (arraySize > 0 ? arraySize : 10);
		ptr = new int[size];
		for (int i = 0; i < size; i++)
			ptr[i] = 0;

	}
           

修改為如上即可

C++中的 explicit 關鍵字

這時就不能調用轉換構造函數了。。。

示例 二:

explicit 構造函數的作用 解析: explicit構造函數是用來防止隐式轉換的。請看下面的代碼:

下面示例展示了如何防止隐式轉換。

#include <iostream>
using namespace std;


class Text{
public:
	explicit Text(string a) :name(a){
		cout << name.c_str() << endl;
	};
	Text() :name(NULL){
		cout << name.c_str() << endl;
	};
private:
	string name;
};

class Text2{
public:
	Text2(string a) :name(a){
		cout << name.c_str() << endl;
	};
	Text2() :name(NULL){
		cout << name.c_str() << endl;
	};
private:
	string name;
};

int main(){
	Text s = "xxxxxx";
	Text s2("xxxxxxx");
	Text2 s3 = "xxxxxxx";


	return 0;
}
           
C++中的 explicit 關鍵字

注釋掉不能隐式轉換後執行結果

C++中的 explicit 關鍵字

繼續閱讀