⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 utf-8, unicode, gb2312格式串转换之-c语言版 - 平凡中的不平凡 - 博客园.htm

📁 最为全面的c++内存模型探秘。不管是初学者还是老手
💻 HTM
📖 第 1 页 / 共 5 页
字号:
style="COLOR: #000000">&nbsp;rhs);<BR>};<BR></SPAN></SPAN></DIV>
<P>&nbsp;</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&amp;,而 
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">&nbsp;Base&nbsp;<BR>{<BR></SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">virtual</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;f(){&nbsp;}<BR>};<BR>&nbsp;<BR></SPAN><SPAN 
style="COLOR: #0000ff">class</SPAN><SPAN 
style="COLOR: #000000">&nbsp;Derived&nbsp;:&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">&nbsp;Base&nbsp;<BR>{&nbsp;<BR></SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;f2()&nbsp;{}<BR>};&nbsp;<BR><BR></SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;main&nbsp;()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;Base&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pB&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">new</SPAN><SPAN 
style="COLOR: #000000">&nbsp;Derived();<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">const</SPAN><SPAN 
style="COLOR: #000000">&nbsp;type_info</SPAN><SPAN 
style="COLOR: #000000">&amp;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;t&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;typeid(</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">pB);cout&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">t.name()&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;delete&nbsp;pB;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;Derived&nbsp;d;<BR>&nbsp;&nbsp;&nbsp;&nbsp;Base</SPAN><SPAN 
style="COLOR: #000000">&amp;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;b&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;d;<BR>&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;typeid(b).name()&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<BR>}</SPAN></SPAN></DIV>
<P>&nbsp;</P>
<P>运行结果:</P>
<P><IMG 
src="UTF-8, Unicode, GB2312格式串转换之-C语言版 - 平凡中的不平凡 - 博客园.files/2009022814025496.png"></P>
<P>&nbsp;</P>
<UL></UL>
<P>4)dynamic_cast,用来运行时的类型转化,需要/GR来正确运行。<BR><SPAN 
style="COLOR: #000000">适用:</SPAN> <SPAN 
style="COLOR: #ff0000"><BR>第一,用于所有的父子和兄弟间指针和引用的转化,有类型安全检查;</SPAN>&nbsp; <SPAN 
style="COLOR: #ff0000"><BR>第二,对指针类型,如果不成功,返回NULL,对引用类型,如果不成功,则抛出异常;</SPAN>&nbsp; 
<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">&nbsp;AA&nbsp;<BR>{<BR></SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">virtual</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;do_sth(){&nbsp;std::cout</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">AA\n</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">;&nbsp;}<BR>};<BR></SPAN><SPAN 
style="COLOR: #0000ff">class</SPAN><SPAN 
style="COLOR: #000000">&nbsp;BB&nbsp;<BR>{<BR></SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">virtual</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;do_sth(){&nbsp;std::cout</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">BB\n</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">;&nbsp;}<BR>};<BR></SPAN><SPAN 
style="COLOR: #0000ff">class</SPAN><SPAN 
style="COLOR: #000000">&nbsp;CC&nbsp;:&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">&nbsp;AA,&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">&nbsp;BB<BR>{<BR></SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">virtual</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;do_sth(){&nbsp;std::cout</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">CC\n</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #000000">;&nbsp;}&nbsp;<BR>};<BR><BR></SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;DynamicCastTest()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;AA&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pA&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">new</SPAN><SPAN 
style="COLOR: #000000">&nbsp;CC;<BR>&nbsp;&nbsp;&nbsp;&nbsp;BB&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pB&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;dynamic_cast</SPAN><SPAN 
style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">BB</SPAN><SPAN 
style="COLOR: #000000">*&gt;</SPAN><SPAN 
style="COLOR: #000000">(pA);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">if</SPAN><SPAN 
style="COLOR: #000000">(pB&nbsp;</SPAN><SPAN 
style="COLOR: #000000">!=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">cast&nbsp;successful!</SPAN><SPAN 
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<BR>&nbsp;&nbsp;&nbsp;&nbsp;CC&nbsp;</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN style="COLOR: #000000">pC&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;dynamic_cast</SPAN><SPAN 
style="COLOR: #000000">&lt;</SPAN><SPAN style="COLOR: #000000">CC</SPAN><SPAN 
style="COLOR: #000000">*&gt;</SPAN><SPAN 
style="COLOR: #000000">(pA);<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">if</SPAN><SPAN 
style="COLOR: #000000">(pC&nbsp;</SPAN><SPAN 
style="COLOR: #000000">!=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;NULL)<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #800000">"</SPAN><SPAN 
style="COLOR: #800000">cast&nbsp;successful!</SPAN><SPAN 
style="COLOR: #800000">"</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #000000">&lt;&lt;</SPAN><SPAN 
style="COLOR: #000000">&nbsp;endl;<BR>}</SPAN></SPAN></DIV>
<P>&nbsp;</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">&nbsp;A<BR>{<BR></SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">:<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">virtual</SPAN><SPAN 
style="COLOR: #000000">&nbsp;</SPAN><SPAN style="COLOR: #000000">~</SPAN><SPAN 
style="COLOR: #000000">A(){}<BR>};<BR></SPAN><SPAN 
style="COLOR: #0000ff">class</SPAN><SPAN 
style="COLOR: #000000">&nbsp;B&nbsp;:&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">public</SPAN><SPAN 
style="COLOR: #000000">&nbsp;A<BR>{<BR>};<BR><BR></SPAN><SPAN 
style="COLOR: #0000ff">void</SPAN><SPAN 
style="COLOR: #000000">&nbsp;ImplicitCast()<BR>{<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">short</SPAN><SPAN 
style="COLOR: #000000">&nbsp;a&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">2000</SPAN><SPAN 
style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN 
style="COLOR: #000000">&nbsp;b;<BR>&nbsp;&nbsp;&nbsp;&nbsp;b&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;a;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">double</SPAN><SPAN 
style="COLOR: #000000">&nbsp;d&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">10.05</SPAN><SPAN 
style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN 
style="COLOR: #000000">&nbsp;i;<BR>&nbsp;&nbsp;&nbsp;&nbsp;i&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;d;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">int</SPAN><SPAN 
style="COLOR: #000000">&nbsp;j&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #800080">75</SPAN><SPAN 
style="COLOR: #000000">;<BR>&nbsp;&nbsp;&nbsp;&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">char</SPAN><SPAN 
style="COLOR: #000000">&nbsp;c;<BR>&nbsp;&nbsp;&nbsp;&nbsp;c&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN 
style="COLOR: #000000">&nbsp;j;<BR><BR>&nbsp;&nbsp;&nbsp;&nbsp;A</SPAN><SPAN 
style="COLOR: #000000">*</SPAN><SPAN 
style="COLOR: #000000">&nbsp;pA&nbsp;</SPAN><SPAN 
style="COLOR: #000000">=</SPAN><SPAN style="COLOR: #000000">&nbsp;</SPAN><SPAN 
style="COLOR: #0000ff">new</SPAN><SPAN 

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -