c++中的explicit 关键字是什么意思?

yuantao 2003-10-11 09:56:05
c++中的explicit 关键字是什么意思?是不是跟virtual base class有关系/
...全文
23528 16 打赏 收藏 转发到动态 举报
写回复
用AI写文章
16 条回复
切换为时间正序
请发表友善的回复…
发表回复
oldjackyone 2004-03-01
  • 打赏
  • 举报
回复
explicit
C++ Specific

This keyword is a declaration specifier that can only be applied to in-class constructor declarations. Constructors declared explicit will not be considered for implicit conversions. For example:
这是一个指定用于类构造函数处声明的一个关键字。在类外部进行构造类中的构造函数,不支持隐式的转换,例如:
class X {
public:
explicit X(int); //legal(合法)
explicit X(double) { //legal(合法)
// ...
}
};

explicit X::X(int) {} //illegal(非法)

An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object. For example, with the class declared above:
外部的构造器不能参与部分的隐式转换,只能明确的构造一个对象,例如,用上面的例子:

void f(X) {}
void g(int I) {
f(I); // will cause error(将会引起错误)
}
void h() {
X x1(1); // legal(合法)
}

The function call f(I) fails because there is no available implicit conversion from int to X.
g()函数中,调用f(I)出错的原因:“没有一个可用的隐式转换可从int到X类型。”

Note It is meaningless to apply explicit to constructors with multiple arguments, since such constructors cannot take part in implicit conversions.
注释:多参数的构造器直接应用explicit关键字是无意义的,应用了explicit后,构造器不能进行部分的隐式转换.
END C++ Specific


翻译的...
deeplives 2004-03-01
  • 打赏
  • 举报
回复
禁止隐式转换,防止程序员误操作
oldjackyone 2004-03-01
  • 打赏
  • 举报
回复
哈,果然。
redhat_xu 2004-03-01
  • 打赏
  • 举报
回复
explicit: 禁止隐式类型转换操作


explicit 它与 virtual、inline 合称为“函数限定符”。它只适用于构造函数。若一个类拥有只带一个参数的构造函数,则可以使用 MyClass object(x) 或 MyClass object = x
来初始化对象,这实际是进行了从参数类型到类类型的转换。若在在构造函数前加上限定符
explicit ,将不再允许这种转换,即不允许 MyClass object = x 这种形式。
sharkhuang 2004-02-29
  • 打赏
  • 举报
回复
防止隐私转换!造成不必要的麻烦
cgsw12345 2004-02-29
  • 打赏
  • 举报
回复
禁止單參數的構造函數隐式转化為參數類型!
kaphoon 2004-02-29
  • 打赏
  • 举报
回复
前面讲的对
使用来禁止隐式转化带来的错误

byyyyy 2004-02-29
  • 打赏
  • 举报
回复
禁止隐式转化带来的错误。
BluntBlade 2004-02-29
  • 打赏
  • 举报
回复
explicit的意图是指明一个函数的调用必须是显式的。
副作用才是禁止隐式的类型转化
Mephisto_76 2004-02-29
  • 打赏
  • 举报
回复
是为了防止单参数的构造函数的隐式转化。比如说有一个类 Rational 表示一个复数,如下:

class Rational
{
public:
int _x;
int _y;

Rational( int x = 0,int y = 0 )
{
_x = x;
_y = y;
}


};

void printValue(const Rational& ra)
{
cout << ra._x << ra._y <<endl;
}

如果是不用explicit 关键字,则象下边的东西也是合法的:

int x;
printValue( x );

如果用了explicit 关键字,这个是不合法的,我们一定要写成如下格式:

printValue( Rational( x ) )才成,这样就避免了隐式转化带来的bug。
Januarius_ 2004-02-29
  • 打赏
  • 举报
回复
禁止隐式转化
paddy102 2004-02-29
  • 打赏
  • 举报
回复
explicit,和构造函数一起使用.
explicit constructor指明构造函数只能显示使用,目的是为了防止不必要的隐式转化.
举个例子:
有这样一段代码:

class A
{
public:
A(int);
private:
int num;
};

int Test(const A&) // 一个应用函数
{
...
}

Test(2); // 正确
过程是这样的: 编译器知道传的值是int而函数需要的是A类型,但它也同时知道调用A的构造函数将int转换成一个合适的A,所以才有上面成功的调用.换句话说,编译器处理这个调用时的情形类似下面这样:
const A temp(2); // 从2产生一个临时A对象
Test(temp); // 调用函数


如果代码写成如下样子:
class A
{
public:
explicit A(int);
private:
int num;
};

int Test(const A&) // 一个应用函数
{
...
}

Test(2); // 失败,不能通过隐式类型转换将int类型变量构造成成A类型变量
tiarts 2004-02-29
  • 打赏
  • 举报
回复
学习ing
answerzy 2004-02-29
  • 打赏
  • 举报
回复
paddy102(天易) 兄
说中要害了
好象《C++编程思想》还是《The C++ Programming Language》上面有
Andy84920 2003-10-11
  • 打赏
  • 举报
回复
explicit主要用于"修饰"构造函数.
使得它不用于程序中需要通过此构造函数进行"隐式"转换的情况!

指定此关键字,需要隐式转换方可进行的程序将会不能通过.
而可通过强制转换使它没有用.

daizh 2003-10-11
  • 打赏
  • 举报
回复
explicit
C++ Specific

This keyword is a declaration specifier that can only be applied to in-class constructor declarations. Constructors declared explicit will not be considered for implicit conversions. For example:

class X {
public:
explicit X(int); //legal
explicit X(double) { //legal
// ...
}
};

explicit X::X(int) {} //illegal
An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object. For example, with the class declared above:

void f(X) {}
void g(int I) {
f(i); // will cause error
}
void h() {
X x1(1); // legal
}
The function call f(i) fails because there is no available implicit conversion from int to X.

Note It is meaningless to apply explicit to constructors with multiple arguments, since such constructors cannot take part in implicit conversions.

END C++ Specific

64,676

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧