﻿<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:trackback="http://madskills.com/public/xml/rss/module/trackback/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/"><channel><title>C++博客-随手写写</title><link>http://www.cppblog.com/zhichong/</link><description>知识，是需要积累的</description><language>zh-cn</language><lastBuildDate>Tue, 09 Jun 2026 19:09:05 GMT</lastBuildDate><pubDate>Tue, 09 Jun 2026 19:09:05 GMT</pubDate><ttl>60</ttl><item><title>explicit关键字</title><link>http://www.cppblog.com/zhichong/archive/2006/09/25/12953.html</link><dc:creator>每次想名字都头痛</dc:creator><author>每次想名字都头痛</author><pubDate>Mon, 25 Sep 2006 13:27:00 GMT</pubDate><guid>http://www.cppblog.com/zhichong/archive/2006/09/25/12953.html</guid><wfw:comment>http://www.cppblog.com/zhichong/comments/12953.html</wfw:comment><comments>http://www.cppblog.com/zhichong/archive/2006/09/25/12953.html#Feedback</comments><slash:comments>0</slash:comments><wfw:commentRss>http://www.cppblog.com/zhichong/comments/commentRss/12953.html</wfw:commentRss><trackback:ping>http://www.cppblog.com/zhichong/services/trackbacks/12953.html</trackback:ping><description><![CDATA[explicit关键字的用处是防止显示转换，废话不说，看例子说话：<br /><br /><div style="border: 1px solid rgb(204, 204, 204); padding: 4px 5px 4px 4px; background-color: rgb(238, 238, 238); font-size: 13px; width: 98%;"><!--<br><br>Code highlighting produced by Actipro CodeHighlighter (freeware)<br>http://www.CodeHighlighter.com/<br><br>--><span style="color: rgb(0, 0, 0);"><br />#include </span><span style="color: rgb(0, 0, 0);">&lt;</span><span style="color: rgb(0, 0, 0);">cstdio</span><span style="color: rgb(0, 0, 0);">&gt;</span><span style="color: rgb(0, 0, 0);"><br /><br /></span><span style="color: rgb(0, 0, 255);">class</span><span style="color: rgb(0, 0, 0);"> A<br />{<br /></span><span style="color: rgb(0, 0, 255);">public</span><span style="color: rgb(0, 0, 0);">:<br />    A(){ printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">A()\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">); }<br />    A(</span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> i):m_i(i){ printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">A(int i)\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">); }<br />    </span><span style="color: rgb(0, 0, 255);">explicit</span><span style="color: rgb(0, 0, 0);"> A(</span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);"> A </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 255);">ref</span><span style="color: rgb(0, 0, 0);">)<br />    {<br />        m_i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 255);">ref</span><span style="color: rgb(0, 0, 0);">.m_i;<br />        printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">A(const A &amp;ref)\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />    }<br />    A </span><span style="color: rgb(0, 0, 0);">&amp;</span><span style="color: rgb(0, 0, 255);">operator</span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);">(</span><span style="color: rgb(0, 0, 255);">const</span><span style="color: rgb(0, 0, 0);"> A</span><span style="color: rgb(0, 0, 0);"> &amp;</span><span style="color: rgb(0, 0, 0);">rhs)<br />    {<br />        printf(</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">A &amp;operator=(const A&amp; rhs)\n</span><span style="color: rgb(0, 0, 0);">"</span><span style="color: rgb(0, 0, 0);">);<br />        m_i </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> rhs.m_i;<br />        </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">*</span><span style="color: rgb(0, 0, 255);">this</span><span style="color: rgb(0, 0, 0);">;<br />    }<br /></span><span style="color: rgb(0, 0, 255);">private</span><span style="color: rgb(0, 0, 0);">:<br />    </span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> m_i;<br />};<br /><br />A func(A a)<br />{<br />    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> a;    <font color="#008000">//A(const A &amp;ref)</font><br />}<br /><br /></span><span style="color: rgb(0, 0, 255);">int</span><span style="color: rgb(0, 0, 0);"> main()<br />{<br />    A a </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;    </span><span style="color: rgb(0, 128, 0);">    //</span><span style="color: rgb(0, 128, 0);"> A(int i)      **</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    A b </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> a;    </span><span style="color: rgb(0, 128, 0);">    //</span><span style="color: rgb(0, 128, 0);"> A(const A &amp;ref)</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    A c;        </span><span style="color: rgb(0, 128, 0);">    //</span><span style="color: rgb(0, 128, 0);"> <img src="http://www.cppblog.com/images/dot.gif" /></span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;        </span><span style="color: rgb(0, 128, 0);">  //</span><span style="color: rgb(0, 128, 0);"> 首先A(int i), 然后 A &amp;operator=(const A &amp;rhs)</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    c </span><span style="color: rgb(0, 0, 0);">=</span><span style="color: rgb(0, 0, 0);"> b;        </span><span style="color: rgb(0, 128, 0);">  //</span><span style="color: rgb(0, 128, 0);"> A &amp;operator=(const A &amp;rhs)</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 0);">func(</span><span style="color: rgb(0, 0, 0);">1</span><span style="color: rgb(0, 0, 0);">);    </span><span style="color: rgb(0, 128, 0);">    //</span><span style="color: rgb(0, 128, 0);"> func(1) ---&gt; A(int i)    **</span><span style="color: rgb(0, 128, 0);"><br /></span><span style="color: rgb(0, 0, 0);">    </span><span style="color: rgb(0, 0, 255);">return</span><span style="color: rgb(0, 0, 0);"> </span><span style="color: rgb(0, 0, 0);">0</span><span style="color: rgb(0, 0, 0);">;<br />}<br /></span></div><br /><br />由于用了explicit关键字修饰，凡是用到A(const A &amp;ref)构造函数的地方都会编译出错。<br /><br />**用explicit声明的A(const A &amp;ref)应该对这个没有影响才对，但是gcc 3.4报错。<br /><br /><img src ="http://www.cppblog.com/zhichong/aggbug/12953.html" width = "1" height = "1" /><br><br><div align=right><a style="text-decoration:none;" href="http://www.cppblog.com/zhichong/" target="_blank">每次想名字都头痛</a> 2006-09-25 21:27 <a href="http://www.cppblog.com/zhichong/archive/2006/09/25/12953.html#Feedback" target="_blank" style="text-decoration:none;">发表评论</a></div>]]></description></item></channel></rss>