C++中定义的 explicit 常在构造函数中使用防止C++的隐式转换,可以阻止不应该允许的经过转换构造函数进行的隐式转换的发生。声明为explicit的构造函数不能在隐式转换中使用。
错误预防技巧11.3:
对于那些不应该被编译器用来执行隐式转换的单参数构造函数,请使用关键字explicit
explicit 关键字在函数声明处使用:
示例:
explicit Array(int = 10);
C++中, 一个参数构造函数
(或者除了第一个参数外其余参数都有默认值的多参构造函数)
, 承担了两个角色。
1 一个参数的构造函数 是个构造器
2 一个参数的构造函数 是个默认且隐含的类型转换操作符。
任何单参数的构造函数都可以看做是转换构造函数。

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;
}
输出:
可以看出我们并没有定义
参数为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;
}
修改为如上即可
这时就不能调用转换构造函数了。。。
示例 二:
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;
}
注释掉不能隐式转换后执行结果