- 1、本文档共5页,可阅读全部内容。
- 2、原创力文档(book118)网站文档一经付费(服务费),不意味着购买了该文档的版权,仅供个人/单位学习、研究之用,不得用于商业用途,未经授权,严禁复制、发行、汇编、翻译或者网络传播等,侵权必究。
- 3、本站所有内容均由合作方或网友上传,本站不对文档的完整性、权威性及其观点立场正确性做任何保证或承诺!文档内容仅供研究参考,付费前请自行鉴别。如您付费,意味着您自己接受本站规则且自行承担风险,本站不退款、不进行额外附加服务;查看《如何避免下载的几个坑》。如果您已付费下载过本站文档,您可以点击 这里二次下载。
- 4、如文档侵犯商业秘密、侵犯著作权、侵犯人身权等,请点击“版权申诉”(推荐),也可以打举报电话:400-050-0827(电话支持时间:9:00-18:30)。
- 5、该文档为VIP文档,如果想要下载,成为VIP会员后,下载免费。
- 6、成为VIP后,下载本文档将扣除1次下载权益。下载后,不支持退款、换文档。如有疑问请联系我们。
- 7、成为VIP后,您将拥有八大权益,权益包括:VIP文档下载权益、阅读免打扰、文档格式转换、高级专利检索、专属身份标志、高级客服、多端互通、版权登记。
- 8、VIP文档为合作方或网友上传,每下载1次, 网站将根据用户上传文档的质量评分、类型等,对文档贡献者给予高额补贴、流量扶持。如果你也想贡献VIP文档。上传文档
查看更多
explicit关键字的作用
谈谈explicit关键字
2004-08-19 20:35 16677人阅读 评论(7) 收藏 举报 今天看到公司的代码内有大量的explicit关键字,但是老版的MSDN内例子并不完善,实在是不明白,最终从网上一篇文章内找到了答案:原来explicit是为了防止隐式使用拷贝构造函数的.以下附上从新版MSDN中找到的例子和网上那篇文章:
// Copy From MSDN
This keyword is a declaration specifier that can only be applied to in-class constructor declarations. An explicit constructor cannot take part in implicit conversions. It can only be used to explicitly construct an object.
The following program will fail to compile because of the explicit keyword. To resolve the error, remove the explicit keywords and adjust the code in g.
// spec1_explicit.cpp
// compile with: /EHsc
#include
class C
{
public:
int i;
explicit C(const C) // an explicit copy constructor
{
printf(/nin the copy constructor);
}
explicit C(int i ) // an explicit constructor
{
printf(/nin the constructor);
}
C()
{
i = 0;
}
};
class C2
{
public:
int i;
explicit C2(int i ) // an explicit constructor
{
}
};
C f(C c)
{ // C2558
c.i = 2;
return c; // first call to copy constructor
}
void f2(C2)
{
}
void g(int i)
{
f2(i); // C2558
// try the following line instead
// f2(C2(i));
}
int main()
{
C c, d;
d = f(c); // c is copied
}
Note???explicit on a constructor with multiple arguments has no effect, since such constructors cannot take part in implicit conversions. However, for the purpose of implicit conversion, explicit will have an effect if a constructor has multiple arguments and all but one of the arguments has a default value.
?
// Copy From Internet Article
Pointer
不看书不知道自己的C++有多差T_T,看到 explicit 时遇到一个问题。请看下面一段程序:class A{public:A(int i) : m_i(i){}int m_i;};int main(){A a = 0;a = 10; // 这里是什么操作?}这个操作产生了一个临时对象。我怀疑是默认赋值运算符 “A operator = (int i){}”,于是重载了一下该运算符,结果确实运行到重载的运算符函数里了,那么临时对象又是如何产生的呢?难道默认的赋值运算符是这样操作的?A operator = (int i){A a(i);return a;}这让我想起了类似的函数操作:void fn(A a){// ...}这里可以直接写fn(10);也是产生了一个临时对象。难道真的是这样吗?忘解惑。
alexeyomux
老兄你用的是哪个编译器?在我印象之中,好像标
您可能关注的文档
- Effect of the smaller masssquared difference for the long baseline neutrino experiments.pdf
- Effect of thickness on bifacial silicon solar cells.pdf
- Effect of Turbulent Fluctuations on Mean Flow ReynoldsAveraging.pdf
- Effect of Thermal Fluctuation on Spectral Function for the TomonagaLuttinger Model.pdf
- Effect of Turfgrass Cover and Irrigation on Soil Mobility and Dissipation of Mefenoxam and.pdf
- Effect of urbanization on the structure and functional traits of remnant subtropical evergreen.pdf
- Effect of wavelength and incident angle in the laser removal of particles from silicon wafers.pdf
- Effective adaptation of a hidden Markov modelbased named entity recognizer for biomedical.pdf
- Effective Action Studies of Quantum Hall Spin Textures.pdf
- Effective chiral theory of mesons, coefficients of ChPT, axialvector symmetry breaking.pdf
- 2024年内蒙古自治区直属机关遴选公务员遴选真题.docx
- 2024年巢湖市社区工作者招聘真题 .docx
- 2025年河南省初中学业水平考试地理试卷真题(含答案详解) (2).pdf
- 塞曼效应偏振态在矿产资源勘查中的应用前景7800字【论文】 .pdf
- 广东广州城建职业学院辅导员招聘笔试真题2024.docx
- 高中阶段“整本书阅读”的教学活动设计.docx
- 2025年黑龙江龙东地区中考语文试卷真题及答案详解(精校打印) .pdf
- 新文科视野下的档案学“大数据概论”课程教学的三维重构与实证研究 .pdf
- 2025秋统编版语文四年级上册3现代诗二首《秋晚的江上》教学设计 .pdf
- 天津统考卷(取,是勇往直前;舍,是权衡的智慧)-2025年中考语文作文真题解读 .pdf
文档评论(0)