六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 36|回复: 0

C++ 注意事项

[复制链接]

升级  13.33%

72

主题

72

主题

72

主题

举人

Rank: 3Rank: 3

积分
240
 楼主| 发表于 2013-1-26 14:33:10 | 显示全部楼层 |阅读模式
<!--  [if gte mso 9]><xml> <w:WordDocument>  <w:View>Normal</w:View>  <w:Zoom>0</w:Zoom>  <w:PunctuationKerning/>  <w:DrawingGridVerticalSpacing>7.8 磅</w:DrawingGridVerticalSpacing>  <w:DisplayHorizontalDrawingGridEvery>0</w:DisplayHorizontalDrawingGridEvery>  <w:DisplayVerticalDrawingGridEvery>2</w:DisplayVerticalDrawingGridEvery>  <w:ValidateAgainstSchemas/>  <w:SaveIfXMLInvalid>false</w:SaveIfXMLInvalid>  <w:IgnoreMixedContent>false</w:IgnoreMixedContent>  <w:AlwaysShowPlaceholderText>false</w:AlwaysShowPlaceholderText>  <w:Compatibility>   <w:SpaceForUL/>   <w:BalanceSingleByteDoubleByteWidth/>   <w:DoNotLeaveBackslashAlone/>   <w:ULTrailSpace/>   <w:DoNotExpandShiftReturn/>   <w:AdjustLineHeightInTable/>   <w:BreakWrappedTables/>   <w:SnapToGridInCell/>   <w:WrapTextWithPunct/>   <w:UseAsianBreakRules/>   <w:DontGrowAutofit/>   <w:UseFELayout/>  </w:Compatibility>  <w:BrowserLevel>MicrosoftInternetExplorer4</w:BrowserLevel> </w:WordDocument></xml><![endif]--><!--  [if gte mso 9]><xml> <w:LatentStyles DefLockedState="false" LatentStyleCount="156"> </w:LatentStyles></xml><![endif]--><!--  [if gte mso 10]><mce:style><!-- /* Style Definitions */ table.MsoNormalTable{mso-style-name:普通表格;mso-tstyle-rowband-size:0;mso-tstyle-colband-size:0;mso-style-noshow:yes;mso-style-parent:"";mso-padding-alt:0cm 5.4pt 0cm 5.4pt;mso-para-margin:0cm;mso-para-margin-bottom:.0001pt;mso-pagination:widow-orphan;font-size:10.0pt;font-family:"Times New Roman";mso-fareast-font-family:"Times New Roman";mso-ansi-language:#0400;mso-fareast-language:#0400;mso-bidi-language:#0400;}--><!-- [endif]-->
#include <iostream>

using namespace std;


class SuperClass

{

public:

SuperClass():itsAge(1){ }

virtual ~SuperClass(){ }

virtual void doSomething() const{ cout << "SuperClassdoSomething!\n"; }


protected:

int itsAge;

};


class SubClass : public SuperClass

{

public:

void doSomething()const { cout<< "SubClass doSomething!\n"; }

};


void PtrFunction (SuperClass* x)

{

x->doSomething();

}

void RefFunction (SuperClass& x)

{

x = (SuperClass)x;

x.doSomething();

}

void ValFunction (SuperClassx)

{

x.doSomething();

}


int main()

{

SuperClass* ptr=0;

ptr = new SubClass;


PtrFunction(ptr);

RefFunction(*ptr);

ValFunction(*ptr); // 此处会调用到超类的doSomeThing方法

(*ptr).doSomething();


SuperClass x = (SuperClass)(*ptr);


x.doSomething();


return 0;

}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表