📄 utf-8, unicode, gb2312格式串转换之-c语言版 - 平凡中的不平凡 - 博客园.htm
字号:
style="COLOR: #000000"> rhs);<BR>};<BR></SPAN></SPAN></DIV>
<P> </P>
<P><SPAN id=Code_Open_Text_135619><SPAN style="COLOR: #000000"><SPAN
style="COLOR: #ff0000">问题:RTTI怎么实现那?对象,type_info,虚函数怎么关联那?《深入C++对象模型》中说在虚函数表的开始存储了类型信息,但是实际的VS2008中好像并没有此信息,请高人指点哦!<BR></SPAN></SPAN></SPAN></P>
<P>3)typeid,在运行时获得对象的类型,typeid()返回的是const type_info&,而
type_info包含了对象真实类型的名字。typeid能被用来获取一个引用对象或指针指向的对象的运行时的真实类型。当然如果对象为null或编译时没有使用/GR的话,typeid的会抛出异常bad_typeid
exception或__non_rtti_object。实例代码:</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_140204 style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_140204').style.display='none'; document.getElementById('Code_Open_Image_140204').style.display='inline'; document.getElementById('Code_Open_Text_140204').style.display='inline';"
height=16
src="UTF-8, Unicode, GB2312格式串转换之-C语言版 - 平凡中的不平凡 - 博客园.files/ContractedBlock.gif"
width=11 align=top><IMG id=Code_Open_Image_140204
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_140204').style.display='none'; getElementById('Code_Closed_Image_140204').style.display='inline'; getElementById('Code_Closed_Text_140204').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_140204>Code</SPAN><SPAN id=Code_Open_Text_140204><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"> Base <BR>{<BR></SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000">:<BR> </SPAN><SPAN
style="COLOR: #0000ff">virtual</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> f(){ }<BR>};<BR> <BR></SPAN><SPAN
style="COLOR: #0000ff">class</SPAN><SPAN
style="COLOR: #000000"> Derived : </SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> Base <BR>{ <BR></SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000">:<BR> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> f2() {}<BR>}; <BR><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> main ()<BR>{<BR> Base </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: #0000ff">new</SPAN><SPAN
style="COLOR: #000000"> Derived();<BR> </SPAN><SPAN
style="COLOR: #0000ff">const</SPAN><SPAN
style="COLOR: #000000"> type_info</SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000"> t </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> typeid(</SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN
style="COLOR: #000000">pB);cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000">t.name() </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR> delete pB;<BR><BR> Derived d;<BR> Base</SPAN><SPAN
style="COLOR: #000000">&</SPAN><SPAN
style="COLOR: #000000"> b </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> d;<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> typeid(b).name() </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR>}</SPAN></SPAN></DIV>
<P> </P>
<P>运行结果:</P>
<P><IMG
src="UTF-8, Unicode, GB2312格式串转换之-C语言版 - 平凡中的不平凡 - 博客园.files/2009022814025496.png"></P>
<P> </P>
<UL></UL>
<P>4)dynamic_cast,用来运行时的类型转化,需要/GR来正确运行。<BR><SPAN
style="COLOR: #000000">适用:</SPAN> <SPAN
style="COLOR: #ff0000"><BR>第一,用于所有的父子和兄弟间指针和引用的转化,有类型安全检查;</SPAN> <SPAN
style="COLOR: #ff0000"><BR>第二,对指针类型,如果不成功,返回NULL,对引用类型,如果不成功,则抛出异常;</SPAN>
<SPAN
style="COLOR: #ff0000"><BR>第三,类型必须要有虚函数,且打开/GR编译选项,否则不能使用dynamic_cast。<BR></SPAN>实例代码:</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_145022 style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_145022').style.display='none'; document.getElementById('Code_Open_Image_145022').style.display='inline'; document.getElementById('Code_Open_Text_145022').style.display='inline';"
height=16
src="UTF-8, Unicode, GB2312格式串转换之-C语言版 - 平凡中的不平凡 - 博客园.files/ContractedBlock.gif"
width=11 align=top><IMG id=Code_Open_Image_145022
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_145022').style.display='none'; getElementById('Code_Closed_Image_145022').style.display='inline'; getElementById('Code_Closed_Text_145022').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_145022>Code</SPAN><SPAN id=Code_Open_Text_145022><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"> AA <BR>{<BR></SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000">:<BR> </SPAN><SPAN
style="COLOR: #0000ff">virtual</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> do_sth(){ std::cout</SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">AA\n</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000">; }<BR>};<BR></SPAN><SPAN
style="COLOR: #0000ff">class</SPAN><SPAN
style="COLOR: #000000"> BB <BR>{<BR></SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000">:<BR> </SPAN><SPAN
style="COLOR: #0000ff">virtual</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> do_sth(){ std::cout</SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">BB\n</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000">; }<BR>};<BR></SPAN><SPAN
style="COLOR: #0000ff">class</SPAN><SPAN
style="COLOR: #000000"> CC : </SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> AA, </SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> BB<BR>{<BR></SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000">:<BR> </SPAN><SPAN
style="COLOR: #0000ff">virtual</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> do_sth(){ std::cout</SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">CC\n</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #000000">; } <BR>};<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> DynamicCastTest()<BR>{<BR> AA </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"> CC;<BR> BB </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pB </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> dynamic_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">BB</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pA);<BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000">(pB </SPAN><SPAN
style="COLOR: #000000">!=</SPAN><SPAN
style="COLOR: #000000"> NULL)<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">cast successful!</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR> CC </SPAN><SPAN
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pC </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> dynamic_cast</SPAN><SPAN
style="COLOR: #000000"><</SPAN><SPAN style="COLOR: #000000">CC</SPAN><SPAN
style="COLOR: #000000">*></SPAN><SPAN
style="COLOR: #000000">(pA);<BR> </SPAN><SPAN
style="COLOR: #0000ff">if</SPAN><SPAN
style="COLOR: #000000">(pC </SPAN><SPAN
style="COLOR: #000000">!=</SPAN><SPAN
style="COLOR: #000000"> NULL)<BR> cout </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN
style="COLOR: #800000">cast successful!</SPAN><SPAN
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #000000"><<</SPAN><SPAN
style="COLOR: #000000"> endl;<BR>}</SPAN></SPAN></DIV>
<P> </P>
<P>二 其他cast</P>
<P>1)隐式转化,不需要任何操作符,转化被自动执行,当一个值被赋值到它所兼容的类型时。<BR><SPAN
style="COLOR: #ff0000"><SPAN
style="COLOR: #000000">适用:</SPAN><BR>第一,内置基本类型的兼容转化;<BR>第二,
子类指针,引用向父类的转化;</SPAN></P>
<P>实例:</P>
<DIV class=cnblogs_code><IMG id=Code_Closed_Image_145421 style="DISPLAY: none"
onclick="this.style.display='none'; document.getElementById('Code_Closed_Text_145421').style.display='none'; document.getElementById('Code_Open_Image_145421').style.display='inline'; document.getElementById('Code_Open_Text_145421').style.display='inline';"
height=16
src="UTF-8, Unicode, GB2312格式串转换之-C语言版 - 平凡中的不平凡 - 博客园.files/ContractedBlock.gif"
width=11 align=top><IMG id=Code_Open_Image_145421
onclick="this.style.display='none'; document.getElementById('Code_Open_Text_145421').style.display='none'; getElementById('Code_Closed_Image_145421').style.display='inline'; getElementById('Code_Closed_Text_145421').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_145421>Code</SPAN><SPAN id=Code_Open_Text_145421><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"> A<BR>{<BR></SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000">:<BR> </SPAN><SPAN
style="COLOR: #0000ff">virtual</SPAN><SPAN
style="COLOR: #000000"> </SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN
style="COLOR: #000000">A(){}<BR>};<BR></SPAN><SPAN
style="COLOR: #0000ff">class</SPAN><SPAN
style="COLOR: #000000"> B : </SPAN><SPAN
style="COLOR: #0000ff">public</SPAN><SPAN
style="COLOR: #000000"> A<BR>{<BR>};<BR><BR></SPAN><SPAN
style="COLOR: #0000ff">void</SPAN><SPAN
style="COLOR: #000000"> ImplicitCast()<BR>{<BR> </SPAN><SPAN
style="COLOR: #0000ff">short</SPAN><SPAN
style="COLOR: #000000"> a </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">2000</SPAN><SPAN
style="COLOR: #000000">;<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> b;<BR> b </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> a;<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">double</SPAN><SPAN
style="COLOR: #000000"> d </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">10.05</SPAN><SPAN
style="COLOR: #000000">;<BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> i;<BR> i </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> d;<BR><BR> </SPAN><SPAN
style="COLOR: #0000ff">int</SPAN><SPAN
style="COLOR: #000000"> j </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000"> </SPAN><SPAN
style="COLOR: #800080">75</SPAN><SPAN
style="COLOR: #000000">;<BR> </SPAN><SPAN
style="COLOR: #0000ff">char</SPAN><SPAN
style="COLOR: #000000"> c;<BR> c </SPAN><SPAN
style="COLOR: #000000">=</SPAN><SPAN
style="COLOR: #000000"> j;<BR><BR> A</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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -