📄 utf-8, unicode, gb2312格式串转换之-c语言版 - 平凡中的不平凡 - 博客园.htm
字号:
style="COLOR: #000000"> B();<BR>}</SPAN></SPAN></DIV>
<P> </P>
<P>2)强制类型转化,即我们常说的C风格的类型转化,基本上可以用于所有的转化,但是没有意义的转化除外,但是父子类,兄弟间的转化没有类型检查可能导致运行是错误。<BR>适用:<BR><SPAN
style="COLOR: #ff0000">第一,基本类型转化;<BR>第二,void*到其他指针的转化;<BR>第三,去除const;<BR>第五,函数指针的转化;<BR>第六,父子类转化,但是多重继承和兄弟转化,可能有运行时错误,没有类型检查;<BR>第七,任何两个类,但是没有实际意义,运行可能出错;<BR>第八,不能用于没有意义的转化,严厉禁止,例如,你不能用static_cast象用C风格的类型转换一样把struct转换成int类型,或者把double类型转换成指针类型;<BR>第九,在C++一般更推荐新加的static_cast,const_cast,dynamic_cast和reinterpret_cast转化方式;</SPAN></P>
<P>实例:</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_150722 style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_150722').style.display='none'; document.getElementById('Code_Open_Image_150722').style.display='inline'; document.getElementById('Code_Open_Text_150722').style.display='inline';"
height=16
src="UTF-8, Unicode, GB2312格式串转换之-C语言版 - 平凡中的不平凡 - 博客园.files/ContractedBlock.gif"
width=11 align=top><IMG id=Code_Open_Image_150722
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_150722').style.display='none'; getElementById('Code_Closed_Image_150722').style.display='inline'; getElementById('Code_Closed_Text_150722').style.display='inline';"
height=16
src="UTF-8, Unicode, GB2312格式串转换之-C语言版 - 平凡中的不平凡 - 博客园.files/ExpandedBlockStart.gif"
width=11 align=top><SPAN class=cnblogs_code_Collapse
id=Code_Closed_Text_150722>Code</SPAN><SPAN id=Code_Open_Text_150722><BR><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN
style="COLOR: #0000ff">class</SPAN><SPAN
style="COLOR: #000000"> CDummy <BR>{<BR></SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000">:<BR> CDummy(</SPAN><SPAN
style="COLOR: #0000ff">float</SPAN><SPAN
style="COLOR: #000000"> x, </SPAN><SPAN
style="COLOR: #0000ff">float</SPAN><SPAN
style="COLOR: #000000"> y)<BR> {<BR> i </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> x;<BR> j </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> y;<BR> }<BR></SPAN><SPAN
style="COLOR: #0000ff">private</SPAN><SPAN
style="COLOR: #000000">:<BR> </SPAN><SPAN
style="COLOR: #0000ff">float</SPAN><SPAN
style="COLOR: #000000"> i,j;<BR>};<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">class</SPAN><SPAN
style="COLOR: #000000"> CAddition <BR>{<BR></SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000">:<BR> CAddition (</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> a, </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> b) { x</SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000">a; y</SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000">b; }<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> result() { </SPAN><SPAN
style="COLOR: #0000ff">return</SPAN><SPAN
style="COLOR: #000000"> x</SPAN><SPAN style="COLOR: #000000">+</SPAN><SPAN
style="COLOR: #000000">y;}<BR></SPAN><SPAN
style="COLOR: #0000ff">private</SPAN><SPAN
style="COLOR: #000000">:<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> x,y;<BR>};<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> Testing()<BR>{<BR> std::cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">Testing</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> std::endl;<BR> </SPAN><SPAN
style="COLOR: #0000ff">return</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800080">10</SPAN><SPAN
style="COLOR: #000000">;<BR>}<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> ExplicitCast()<BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">double</SPAN><SPAN
style="COLOR: #000000"> r </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN
style="COLOR: #0000ff">double</SPAN><SPAN style="COLOR: #000000">)</SPAN><SPAN
style="COLOR: #800080">1</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">/</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">3</SPAN><SPAN
style="COLOR: #000000">;<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pi </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #800080">10</SPAN><SPAN
style="COLOR: #000000">);<BR> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pV;<BR> pV </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> pi;<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pj </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)pV; </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 或 int *pj = int*(pV);</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"><BR> </SPAN><SPAN
style="COLOR: #0000ff">const</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> pa </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #800080">20</SPAN><SPAN
style="COLOR: #000000">);<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pb;<BR> pb </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> (</SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">)pa;<BR> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pb </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">30</SPAN><SPAN
style="COLOR: #000000">;<BR> std::cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pa </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> std::endl;<BR><BR> typedef </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> (</SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">Fun)();<BR><BR> Fun f </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> (Fun)Testing;<BR> f();<BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 多重继承或将兄弟间的转化可能会出错<BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 虽然可以正确的编译,但是运行有问题,所以我们不做没有意义的转化<BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">CDummy d(10,30);<BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">CAddition * padd;<BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">padd = (CAddition*) &d;<BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">std::cout << padd->result();<BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> 不做没有意义的转化</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #808080">///</SPAN><SPAN
style="COLOR: #008000">/ error</SPAN><SPAN
style="COLOR: #808080"><BR></SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">struct st{int i; double d;};<BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">st s;<BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">int x = (int)s; </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">c2440<BR><BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">double y = 10.0;<BR> </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000">int *p = (int*)y; </SPAN><SPAN
style="COLOR: #008000">//</SPAN><SPAN
style="COLOR: #008000"> c2440</SPAN><SPAN
style="COLOR: #008000"><BR></SPAN><SPAN
style="COLOR: #000000">}</SPAN></SPAN></DIV>
<P> </P>
<P>3)static_cast在功能上基本上与C风格的类型转换一样强大,含义也一样。<BR>它也有功能上限制:<BR><SPAN
style="COLOR: #ff0000">第一,不能兄弟间转化,父子间转化没有类型安全检查,有可能会导致运行时错误,父子兄弟的动态转化应该适用dynamic_cast;<BR>第二,不能去除const,适用专用的const_cast;<BR>第三,不能用于两个没有继承关系的类,当然实际上这样的转化也是没有意义的;<BR>第四,当然也不支持没有意义的转化,例如,你不能用static_cast象用C风格的类型转换一样把struct转换成int类型,或者把double类型转换成指针类型;</SPAN></P>
<P>4)const_cast,用来修改类型的const或volatile属性。</P>
<P>适用:<BR><SPAN
style="COLOR: #ff0000">第一,常量指针被转化成非常量指针,并且仍然指向原来的对象;<BR>第二,常量引用被转换成非常量引用,并且仍然指向原来的对象;<BR>第三,常量对象被转换成非常量对象;</SPAN></P>
<P>实例:</P>
<P> </P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_151441 style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_151441').style.display='none'; document.getElementById('Code_Open_Image_151441').style.display='inline'; document.getElementById('Code_Open_Text_151441').style.display='inline';"
height=16
src="UTF-8, Unicode, GB2312格式串转换之-C语言版 - 平凡中的不平凡 - 博客园.files/ContractedBlock.gif"
width=11 align=top><IMG id=Code_Open_Image_151441
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_151441').style.display='none'; getElementById('Code_Closed_Image_151441').style.display='inline'; getElementById('Code_Closed_Text_151441').style.display='inline';"
height=16
src="UTF-8, Unicode, GB2312格式串转换之-C语言版 - 平凡中的不平凡 - 博客园.files/ExpandedBlockStart.gif"
width=11 align=top><SPAN class=cnblogs_code_Collapse
id=Code_Closed_Text_151441>Code</SPAN><SPAN id=Code_Open_Text_151441><BR><!--<br /><br />Code highlighting produced by Actipro CodeHighlighter (freeware)<br />http://www.CodeHighlighter.com/<br /><br />--><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> ConstCastTest()<BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">const</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000"> pa </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">new</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000">(</SPAN><SPAN
style="COLOR: #800080">20</SPAN><SPAN
style="COLOR: #000000">);<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pb;<BR> pb </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> const_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pa);<BR> </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pb </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">30</SPAN><SPAN
style="COLOR: #000000">;<BR> std::cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pa </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> std::endl;<BR>}</SPAN></SPAN></DIV>
<P> </P>
<P>5)reinterpret_cast,此转型操作符的结果取决于编译器,用于修改操作数类型,非类型安全的转换符。<BR>适用:<BR><SPAN
style="COLOR: #ff0000">一般不推荐使用,但是一般用来对函数指针的转化。<BR></SPAN>实例:</P>
<P> </P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_151722 style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_151722').style.display='none'; document.getElementById('Code_Open_Image_151722').style.display='inline'; document.getElementById('Code_Open_Text_151722').style.display='inline';"
height=16
src="UTF-8, Unicode, GB2312格式串转换之-C语言版 - 平凡中的不平凡 - 博客园.files/ContractedBlock.gif"
width=11 align=top><IMG id=Code_Open_Image_151722
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_151722').style.display='none'; getElementById('Code_Closed_Image_151722').style.display='inline'; getElementById('Code_Closed_Text_151722').style.display='inline';"
height=16
src="UTF-8, Unicode, GB2312格式串转换之-C语言版 - 平凡中的不平凡 - 博客园.files/ExpandedBlockStart.gif"
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -