📄 c++ builder和托管c++中属性的使用建议.htm
字号:
<P>//SampleClassUnit.cpp文件<BR>//---------------------------------------------------------------------------</P>
<P><BR>#pragma hdrstop</P>
<P>#include "SampleClassUnit.h"</P>
<P>#pragma package(smart_init)</P>
<P>//---------------------------------------------------------------------------<BR>__fastcall
TBasic::TBasic()<BR>{<BR> m_iNum = 10;</P>
<P> m_pNum = new int(100);<BR>}</P>
<P>__fastcall TBasic::~TBasic()<BR>{<BR> delete
m_pNum;<BR> m_pNum = NULL;<BR>}</P>
<P>//---------------------------------------------------------------------------<BR>__fastcall
THeritor::THeritor()<BR>{<BR> m_pBasic = new
TBasic();<BR>}</P>
<P>__fastcall THeritor::~THeritor()<BR>{<BR>
delete m_pBasic;<BR> m_pBasic = NULL;<BR>}</P>
<P>//---------------------------------------------------------------------------<BR>TBasic
__fastcall THeritor::GetBasicObject()<BR>{<BR>
return m_Basic;<BR>}</P>
<P>TBasic * __fastcall
THeritor::GetBasicPointer()<BR>{<BR> return
m_pBasic;<BR>}</P>
<P>TBasic & __fastcall
THeritor::GetBasicReference()<BR>{<BR> return
m_BasicReference;<BR>}<BR>//---------------------------------------------------------------------------</P>
<P>//测试函数<BR>void __fastcall TSampleForm::Button1Click(TObject
*Sender)<BR>{<BR> //指针属性<BR> if
(m_pHeritor != NULL)<BR>
{<BR> TBasic * pBasic =
m_pHeritor->BasicPointer;<BR>
int iNum =
pBasic->m_iNum;<BR>
RichEdit1->Lines->Add("m_iNum=" +
IntToStr(iNum));<BR> if
(pBasic->m_pNum !=
NULL)<BR>
{<BR>
int ipNum =
*(pBasic->m_pNum);<BR>
RichEdit1->Lines->Add("m_pNum=" +
IntToStr(ipNum));<BR>
}<BR>
else<BR>
{<BR>
RichEdit1->Lines->Add("m_pNum=NULL!");<BR>
}<BR>
}<BR>}<BR>//---------------------------------------------------------------------------</P>
<P>void __fastcall TSampleForm::Button2Click(TObject
*Sender)<BR>{<BR> //引用属性<BR> if
(m_pHeritor != NULL)<BR>
{<BR> TBasic * pBasic =
&(m_pHeritor->BasicReference);<BR>
int iNum =
pBasic->m_iNum;<BR>
RichEdit1->Lines->Add("m_iNum=" +
IntToStr(iNum));<BR> if
(pBasic->m_pNum !=
NULL)<BR>
{<BR>
int ipNum =
*(pBasic->m_pNum);<BR>
RichEdit1->Lines->Add("m_pNum=" +
IntToStr(ipNum));<BR>
}<BR>
else<BR>
{<BR>
RichEdit1->Lines->Add("m_pNum=NULL!");<BR>
}<BR>
}<BR>}<BR>//---------------------------------------------------------------------------</P>
<P>void __fastcall TSampleForm::Button3Click(TObject
*Sender){<BR> //对象属性<BR> if
(m_pHeritor != NULL)<BR>
{<BR> TBasic * pBasic =
&(m_pHeritor->BasicObject);<BR>
int iNum =
pBasic->m_iNum;<BR>
RichEdit1->Lines->Add("m_iNum=" +
IntToStr(iNum));<BR> if
(pBasic->m_pNum !=
NULL)<BR>
{<BR>
int ipNum =
*(pBasic->m_pNum);<BR>
RichEdit1->Lines->Add("m_pNum=" +
IntToStr(ipNum));<BR>
}<BR>
else<BR>
{<BR>
RichEdit1->Lines->Add("m_pNum=NULL!");<BR>
}<BR>
}<BR>}<BR>//---------------------------------------------------------------------------<BR>void
__fastcall TSampleForm::Button4Click(TObject
*Sender)<BR>{<BR> //指针<BR> if
(m_pHeritor != NULL)<BR>
{<BR> TBasic * pBasic =
m_pHeritor->m_pBasic;<BR>
int iNum =
pBasic->m_iNum;<BR>
RichEdit1->Lines->Add("m_iNum=" +
IntToStr(iNum));<BR> if
(pBasic->m_pNum !=
NULL)<BR>
{<BR>
int ipNum =
*(pBasic->m_pNum);<BR>
RichEdit1->Lines->Add("m_pNum=" +
IntToStr(ipNum));<BR>
}<BR>
else<BR>
{<BR>
RichEdit1->Lines->Add("m_pNum=NULL!");<BR>
}<BR>
}<BR>}<BR>//---------------------------------------------------------------------------</P>
<P>void __fastcall TSampleForm::Button5Click(TObject
*Sender)<BR>{<BR> //引用<BR> if
(m_pHeritor != NULL)<BR>
{<BR> TBasic * pBasic =
&(m_pHeritor->m_BasicReference);<BR>
int iNum =
pBasic->m_iNum;<BR>
RichEdit1->Lines->Add("m_iNum=" +
IntToStr(iNum));<BR> if
(pBasic->m_pNum !=
NULL)<BR>
{<BR>
int ipNum =
*(pBasic->m_pNum);<BR>
RichEdit1->Lines->Add("m_pNum=" +
IntToStr(ipNum));<BR>
}<BR>
else<BR>
{<BR>
RichEdit1->Lines->Add("m_pNum=NULL!");<BR>
}<BR>
}<BR>}<BR>//---------------------------------------------------------------------------</P>
<P>void __fastcall TSampleForm::Button6Click(TObject
*Sender)<BR>{<BR> //对象<BR> if
(m_pHeritor != NULL)<BR>
{<BR> TBasic * pBasic =
&(m_pHeritor->m_Basic);<BR>
int iNum =
pBasic->m_iNum;<BR>
RichEdit1->Lines->Add("m_iNum=" +
IntToStr(iNum));<BR> if
(pBasic->m_pNum !=
NULL)<BR>
{<BR>
int ipNum =
*(pBasic->m_pNum);<BR>
RichEdit1->Lines->Add("m_pNum=" +
IntToStr(ipNum));<BR>
}<BR>
else<BR>
{<BR>
RichEdit1->Lines->Add("m_pNum=NULL!");<BR>
}<BR>
}<BR>}<BR>//---------------------------------------------------------------------------</P>
<P> </P>
<P>托管C++中:<BR>
当你的类用于WebService中的时候,当WebService客户端向服务器请求该类的一个对象时,服务器会把该类的相关数据格式化为XML文件传到客户端,其中包括所有属性的值.如果属性包括复杂类型时,可能导致属性中有对象,对象中又有属性,以至于嵌套很深,服务器格式化对象数据为XML文档就要花费很长时间,</P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P> </P>
<P><BR>
1.属性最好不要用于返回复杂对象上.如属性得到的是自定义的对象,或者一些带有指针的对象.这种情况最好是返回对象的指针或者对象的应用的属性.因为当返回对象的属性,会调用对象的拷贝构造函数,一来系统开销大,二来当通过属性得到的对象的副本在超出其作用域时析构时可能调用你自己的析构函数将对象中的指针所指的对象析构调.</P></SPAN></SPAN>
<DIV class=Message id=Message></DIV></FONT></DIV>
<DIV></DIV></TD></TR>
<TR>
<TD
style="PADDING-RIGHT: 10px; DISPLAY: block; PADDING-LEFT: 10px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px"
align=right bgColor=#f7f7f7 height=25>[
<SCRIPT language=JavaScript
src="C++ Builder和托管C++中属性的使用建议.files/Hits.htm"></SCRIPT>
] [<A href="javascript:history.go(-1)">返回上一页</A>] [<A
href="javascript:window.print()">打 印</A>] [<A
href="http://www.malegebide.com/user/favorite.asp?action=add&topic=C++ Builder和托管C++中属性的使用建议">收
藏</A>]</TD></TR>
<TR>
<TD
style="PADDING-RIGHT: 10px; DISPLAY: block; PADDING-LEFT: 10px; PADDING-BOTTOM: 0px; PADDING-TOP: 0px">
<DIV>上一篇文章:<A
href="http://www.malegebide.com/article/1/14/2006/2006092518458.html">C/C++移位运算符出界后的结果是不可预期的</A></DIV>
<DIV>下一篇文章:<A
href="http://www.malegebide.com/article/1/14/2006/2006092518460.html">用C++Builder
建立数据库VCL使用经验</A></DIV></TD></TR></TBODY></TABLE>
<TABLE cellSpacing=0 cellPadding=0 width=575 border=0>
<TBODY>
<TR>
<TD class=titlebg1>∷相关文章评论∷ (评论内容只代表网友观点,与本站立场无关!) [<A
href="http://www.malegebide.com/article/comment.asp?ArticleID=18459"
target=_blank>更多评论</A>…]</TD></TR>
<TR vAlign=top>
<TD></TD></TR></TBODY></TABLE></TD></TR></TBODY></TABLE><!-- 页面底部开始 -->
<TABLE class=tableborder cellSpacing=0 cellPadding=0 width=778 align=center
border=0>
<TBODY>
<TR>
<TD height=5></TD></TR>
<TR>
<TD class=linebar height=8></TD></TR>
<TR>
<TD height=5></TD></TR>
<TR>
<TD class=tablebody align=middle height=65>QQ:310646 <A class=navmenu
href="http://www.malegebide.com/admin/admin_login.asp" target=_blank>A</A>
Copyright © 2006 <FONT face=Verdana, sans-serif Helvetica,
Arial,><B>Malegebide.Com</B></FONT>.
<SCRIPT language=javascript
src="C++ Builder和托管C++中属性的使用建议.files/529370.js"
type=text/javascript></SCRIPT>
No Rights Reserved <BR></TD></TR>
<TR>
<TD height=5></TD></TR></TBODY></TABLE>
<SCRIPT language=javascript
src="C++ Builder和托管C++中属性的使用建议.files/Std_StranJF.Js"></SCRIPT>
<!-- 页面底部结束 -->
<SCRIPT src="C++ Builder和托管C++中属性的使用建议.files/count.htm"
type=text/javascript></SCRIPT>
</BODY></HTML>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -