📄 0516002.htm
字号:
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
<meta name="GENERATOR" content="Microsoft FrontPage 4.0">
<meta name="ProgId" content="FrontPage.Editor.Document">
<title></title>
<link rel="stylesheet" type="text/css" href="../../vckbase.css">
</head>
<body>
<div align="justify">
<table border="0" width="100%" class="font" height="57">
<tr>
<td width="27%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
<font color="#800080">VC知识库(五)</font>
</td>
<td width="73%" height="6" class="bigfont" bgcolor="#B8CFE7" align="center" bordercolor="#800080">
<font color="#800080">www.vckbase.com</font>
</td>
</tr>
<tr>
<td width="100%" height="4" class="header" valign="top" align="center" colspan="2">
<hr>
</td>
</tr>
<tr>
<td width="100%" height="17" class="header" valign="top" align="center" colspan="2">
<span class="op">
<p align="center"><strong>CRuntimeClass的应用</strong></p>
</span>
</td>
</tr>
<tr>
<td width="100%" height="17" class="info" align="center" colspan="2">
<strong><span class="op">闻怡洋</span></strong>
</td>
</tr>
<tr>
<td width="100%" height="22" class="font" colspan="2">
<hr>
</td>
</tr>
<tr>
<td width="100%" height="5" class="font" colspan="2">
<p><span class="op">CRuntimeClass在MFC中的作用很重要,因为MFC利用它来进行类的动态确定,即是通过类变量来判定该变量是否为某一类的实例。由于指针的类型是可以转换的,所以时常会出现从A到B的转换导致错误。而在MFC的各种书籍中对CRuntimeClass的介绍是比较少的,在这里总结它的一些用法。</span></p>
<p><span class="op">1、动态确定类</span></p>
<p><span class="op">在MFC中CObject::IsKindOf(</span> <span class="op">const</span> <span
class="op">CRuntimeClass*</span> <span class="p">pClass</span> <span class="op">)</span> <span
class="op">利用CRuntimeClass来进行判定,如果你生成的类是以CObject为基础的,你可以使用该成员函数来判定。下面举一个例子来加深了解。</span></p>
<pre>class CAge:public CObject
BOOL IsAge(CObject* pO)
{
return pO->IsKindOf( RUNTIME_CLASS( CAge ) );
}
BOOL IsAge2(CAge* pO)
{
return pO->IsKindOf( RUNTIME_CLASS( CAge ) );
}
void main(void)
{
CObject a;
CAge b;
IsAge(&a);//return FALSE
IsAge(&b);//return TRUE
IsAge2(<font
color="#FF0000">(CAge*)</font>&a);//return FALSE,避免强制转换带来的错误
}
</pre>
<p>2、生成类</p>
<p>CObject CRuntimeClass::CreateObject(void)可以产生一个类变量。作用和new类似,但在某些特殊场合有独特的作用。<span
class="op">下面举一个例子来加深了解。</span></p>
<pre>
假定有以下几个类定义
class CWndA: public CWnd
class CWndB: public CWnd
function1()
{
CRuntimeClass* pC=RUNTIME_CLASS( CWndA );
CreateWnd(pC);
}
CWnd* CreateWnd(CRuntimeClass* pClass)
{
return (CWnd*)pClass->CreateObject();
}
</pre>
<p>在上面例子中,CreateWnd返回的是CWnd* 其实它是一个CWndA*。你可以进行由父类到子类的强制转换而不必要担心出错。<font
color="#FF0000">使用CRuntimeClass可以代替使用switch生产类实例的一些繁琐</font>。(请好好想想它的用途,当你发现它的好处时,你一定会大吃一惊,M$使用宏来实现类的动态检测,如果谁有兴趣可以去看看MFC的源代码。)</p>
<span class="op">
<p>注意:在类的定义中使用IMPLEMENT_DYNCREATE后方可生效。</p>
</span>
</td>
</tr>
<tr>
<td width="100%" height="12" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="6" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="8" class="font" colspan="2">
</td>
</tr>
<tr>
<td width="100%" height="17" class="font" colspan="2"></td>
</tr>
</table>
</div>
</body>
</html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -