C++中int m = (l + r) >> 1;的含义

C++中int m = (l + r) >> 1;的含义

int m = (l + r) >> 1;是指l+r的值右移一位,也就相当于l+r的值除以2取整。即(l+r)/2

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
#include <graphics.h> #include <stdlib.h> #include <dos.h> #include <conio.h> #include <math.h> #include <malloc.h> #define G 9.8 /*重力加速度*/ #define PI 3.141593 /*圆周率*/ #define L1 60 /*小屋运动的范围*/ #define T1 100 #define R1 200 #define B1 450 #define AMD1 5 /*修订数*/ #define AMD2 1.78 /*修订数*/ /*鼠标信息宏定义*/ #define WAITING 0xff00 #define LEFTPRESS 0xff01 #define LEFTCLICK 0xff10 #define LEFTDRAG 0xff19 #define RIGHTPRESS 0xff02 #define RIGHTCLICK 0xff20 #define RIGHTDRAG 0xff2a #define MIDDLEPRESS 0xff04 #define MIDDLECLICK 0xff40 #define MIDDLEDRAG 0xff4c #define MOUSEMOVE 0xff08 int Keystate; int MouseExist; int MouseButton; int MouseX; int MouseY; int up[16][16],down[16][16],mouse_draw[16][16],pixel_save[16][16]; void MouseMath()/*计算鼠标的样子*/ { int i,j,jj,k; long UpNum[16]={ 0x3fff,0x1fff,0x0fff,0x07ff, 0x03ff,0x01ff,0x00ff,0x007f, 0x003f,0x00ff,0x01ff,0x10ff, 0x30ff,0xf87f,0xf87f,0xfc3f }; long DownNum[16]={ 0x0000,0x7c00,0x6000,0x7000, 0x7800,0x7c00,0x7e00,0x7f00, 0x7f80,0x7e00,0x7c00,0x4600, 0x0600,0x0300,0x0300,0x0180 }; for(i=0;i<16;i++) { j=jj=15; while(UpNum[i]!=0) { up[i][j]=UpNum[i]%2; j--; UpNum[i]/=2; } while(DownNum[i]!=0) { down[i][jj--]=DownNum[i]%2; DownNum[i]/=2; } for(k=j;k>=0;k--) up[i][k]=0; for(k=jj;k>=0;k--) down[i][k]=0; for(k=0;k<16;k++)/*四种组合方式*/ { if(up[i][k]==0&&down;[i][k]==0) mouse_draw[i][k]=1; else if(up[i][k]==0&&down;[i][k]==1) mouse_draw[i][k]=2; else if(up[i][k]==1&&down;[i][k]==0) mouse_draw[i][k]=3; else mouse_draw[i][k]=4; } } mouse_draw[1][2]=4;/*特殊点*/ } /*鼠标光标显示*/ void MouseOn() { int x=MouseX,y=MouseY; int i,j; int color; for(i=0;i<16;i++)/*画鼠标*/ { for(j=0;j<16;j++) { pixel_save[i][j]=getpixel(x+j,y+i);/*保存原来的颜色*/ if(mouse_draw[i][j]==1) putpixel(x+j,y+i,0); else if(mouse_draw[i][j]==2) putpixel(x+j,y+i,15); } } } /*隐藏鼠标*/ void MouseOff() { int i,j,x,y,color; x=MouseX; y=MouseY; for(i=0;i<16;i++)/*原位置异或消去*/ for(j=0;j<16;j++) { if(mouse_draw[i][j]==3||mouse_draw[i][j]==4) continue; color=getpixel(x+j,y+i); putpixel(x+j,y+i,color^color); putpixel(x+j,y+i,pixel_save[i][j]); } } /*鼠标状态值初始化*/ void MouseReset() { _AX=0x00; geninterrupt(0x33); } /*设置鼠标左右边界 lx:左边界 rx:右边界 */ void MouseSetX(int lx,int rx) { _CX=lx; _DX=rx; _AX=0x07; geninterrupt(0x33); } /*设置鼠标上下边界 uy:上边界 dy:下边界 */ void MouseSetY(int uy,int dy) { _CX=uy; _DX=dy; _AX=0x08; geninterrupt(0x33); } /*设置鼠标当前位置 x:横向坐标 y:纵向坐标 */ void MouseSetXY(int x,int y) { _CX=x; _DX=y; _AX=0x04; geninterrupt(0x33); } /*获取鼠标按下键的信息*/ /*是否按下左键 返回值: 1=按下 0=释放*/ int LeftPress() { _AX=0x03; geninterrupt(0x33); return(_BX&1); } /*是否按下键 返回值同上 */ int MiddlePress() { _AX=0x03; geninterrupt(0x33); return(_BX&4); } /*是否按下右键 返回值同上 */ int RightPress() { _AX=0x03; geninterrupt(0x33); return(_BX&2); } /*获取鼠标当前位置*/ void MouseGetXY() { _AX=0x03; geninterrupt(0x33); MouseX=_CX; MouseY=_DX; } /*鼠标按键情况,返回0表示只移动,返回1表示左右键同时按下,2表示只按了左键,3表示只按了右键*/ int MouseStatus() { int x,y; int status; int press=0; int i,j,color; status=0;/*默认鼠标没有移动*/ x=MouseX; y=MouseY; while(x==MouseX&&y==MouseY&&status;==0&&press;==0) { if(LeftPress()&&RightPress;()) press=1; else if(LeftPress()) press=2; else if(RightPress()) press=3; MouseGetXY(); if(MouseX!=x||MouseY!=y) status=1; } if(status)/*移动情况才重新显示鼠标*/ { for(i=0;i<16;i++)/*原位置异或消去*/ for(j=0;j<16;j++) { if(mouse_draw[i][j]==3||mouse_draw[i][j]==4) continue; color=getpixel(x+j,y+i); putpixel(x+j,y+i,color^color); putpixel(x+j,y+i,pixel_save[i][j]); } MouseOn();/*新位置显示*/ } if(press!=0)/*有按键的情况*/ return press; return 0;/*只移动的情况*/ } /*定义玩家的结构体*/ struct Ren{ int x,y; int life; int color; int lr;/*1表示左,2表示右。*/ }; /*绘制游戏界面*/ void Desktop() { setcolor(14); line(320,0,320,480); rectangle(L1-20,T1-40,R1+20,B1+10); rectangle(640-(R1+20),(T1-40),640-(L1-20),B1+10); outtextxy(25,20,"P1"); outtextxy(345,20,"P2"); } /*把一个数字n转换成字符串,并存储在a,带符号+-*/ void numtostr(int n,char a[5]) { int w,e; e=n; n=abs(n); a[3]=(n)+'0'; w=n/10; a[2]=(w)+'0'; w=w/10; a[1]=(w)+'0'; a[4]='\0'; if(e<0) a[0]='-'; else a[0]='+'; } /*把速度和角度装换成字符串输出*/ void AngleSpeed(double s,double angle) { int ss,aa; char zzs[5],zza[5]; int left,top,right,bottom; left=275; top=50; right=left+90; bottom=top+10; ss=(int)(s); aa=(int)((angle)*180/PI); numtostr(ss,zzs); numtostr(aa,zza); setfillstyle(1,15); setcolor(10); bar(left,top,right,bottom); outtextxy(left+5,top+3,zzs); outtextxy((left+right)/2+5,top+3,zza); circle(right-6,top+3,2); } /*实现人机对抗的函数*/ void Fire (int a[4],double *v,double *angle,int n)/*a数组存放对射的两点,v和angle存放机器射击的角度和速度,n表式机器射击的准确度*/ { int t; double vx,vy; double sx,sy; int m; m=12*4/n; randomize(); m=random(m)-m/2; t=20; sx=(double)(a[2]-a[0]); sy=(double)(a[3]-a[1]); vx=sx/(double)(t); vy=(sy-0.5*PI*(double)(t*t))/(double)(t); *angle=atan((-vy)/vx); *v=sqrt(vx*vx+vy*vy); *v=(*v)*(AMD2+0.01*(double)(m)); AngleSpeed(*v,*angle); } /*绘制生命线的函数*/ void LifePicture(int life,int color,int location) { char lm[5]; int l,t,r,b; l=50; t=20; r=l+200; b=t+10; numtostr(life,lm); setfillstyle(1,color); setcolor(15); if(location==1||location==3) { bar(l,t,r,b); setfillstyle(1,4); bar(l,t+(b-t)/4,l+life,t+3*(b-t)/4); setfillstyle(1,color); bar(r+10,t,r+50,b); outtextxy(r+10+5,t+2,lm); } else { l=320+50; r=l+200; bar(l,t,r,b); setfillstyle(1,4); bar(l,t+(b-t)/4,l+life,t+3*(b-t)/4); setfillstyle(1,color); bar(r+10,t,r+50,b); outtextxy(r+10+5,t+2,lm); } } /*绘制小屋的函数*/ void RenPicture(int x,int y,int color) { setcolor(color); setwritemode(1); line(x,y-40,x-10,y-30);/*画头*/ line(x,y-40,x+10,y-30); line(x-10,y-30,x+10,y-30); line(x-5,y-30,x-5,y-10);/*画脖子*/ line(x+5,y-30,x+5,y-10); line(x-20,y-10,x+20,y-10);/*画身子*/ line(x-20,y+10,x+20,y+10); line(x-20,y-10,x-20,y+10); line(x+20,y-10,x+20,y+10); } /*绘制箭的函数*/ void PictureBullets (int wx,int wy,int tx,int ty) { setcolor(RED); line(wx,wy,tx,ty); line(wx-1,wy-1,tx,ty); line(wx+1,wy+1,tx,ty); } /*绘制小屋上箭的函数*/ void InitialArrow (int x,int y,int a[4]) { int addx,addy; addx=(a[2]-a[0])/6; addy=(a[3]-a[1])/6; PictureBullets(x+addx,y+addy,x,y); } /*判断点qx,qy在直线的什么位置*/ int PointPlace(int qx,int qy,int x1,int y1,int x2,int y2)/*返回0表示在直线上,当斜率存在时:1表示在直线的上面,2表示在直线的下面,当斜率不存在时:3表示在左面,4表示在右面*/ { int s; if(x1==x2) { if(qx<x1) return 3; else if(qx>x1) return 4; else return 0; } else { s=(int)(((double)(y1-y2))/((double)(x1-x2))*((double)(qx-x1))+(double)(y1)); if(qy<s) return 1; else if(qy>s) return 2; else return 0; } } /*根据两点坐标计算出两点距离和斜率。*/ void DistanceAngle (int twoxy1[4],double *distance,double *angle) { double a,b; if(twoxy1[0]!=twoxy1[2]) { a=(double)((double)((double)twoxy1[3]-(double)twoxy1[1])/(double)((double)twoxy1[0]-(double)twoxy1[2])); *angle=atan(a); if(twoxy1[0]<twoxy1[2]) *angle=PI+(*angle); } else if(twoxy1[1]<twoxy1[3]) *angle=PI/2; else *angle=-PI/2; b=(double)((double)(twoxy1[3]-twoxy1[1])*(double)(twoxy1[3]-twoxy1[1])+(double)(twoxy1[2]-twoxy1[0])*(double)(twoxy1[2]-twoxy1[0])); *distance=sqrt(b); } /*由速度角度算sx,sy随时间的变化*/ void RelativePosition(int *sx,int *sy,double v,double angle,double t) { *sx=(int)((v*cos(angle))*t); *sy=(int)((v*sin(angle))*t-0.5*G*t*t); } /*用鼠标画一条直线,把直线的两点坐标放在twoxy数组内。*/ void TwoPoints(int twoxy[4],int dx,int dy) { int i,q=1; int ddx=dx,ddy=dy; double speed=0.0,angle=0.0; twoxy[0]=0; twoxy[1]=0; setcolor(13); line(dx,dy+30,dx,dy-30); line(dx-30,dy,dx+30,dy); setcolor(4); MouseOn();/*显示鼠标*/ setwritemode(1); i=0; while(q) { if(i==1) { MouseOff(); DistanceAngle(twoxy,&speed;,&angle;); AngleSpeed(speed/AMD1,angle); InitialArrow (ddx,ddy,twoxy); setcolor(4); line(twoxy[0],twoxy[1],twoxy[2],twoxy[3]); MouseOn(); if((twoxy[2]!=MouseX)||(twoxy[3]!=MouseY)) { twoxy[2]=MouseX; twoxy[3]=MouseY; MouseOff(); DistanceAngle(twoxy,&speed;,&angle;); AngleSpeed(speed/AMD1,angle); InitialArrow (ddx,ddy,twoxy); setcolor(4); line(twoxy[0],twoxy[1],twoxy[2],twoxy[3]); MouseOn(); } } if(MouseStatus()) { sound(1000);/*响声函数*/ delay(10000); nosound(); delay(1000000); delay(1000000); delay(1000000); delay(1000000); if(i==0) { twoxy[0]=MouseX; twoxy[1]=MouseY; twoxy[2]=MouseX; twoxy[3]=MouseY; i=1; } else { MouseOff(); DistanceAngle(twoxy,&speed;,&angle;); AngleSpeed(speed/AMD1,angle); InitialArrow (ddx,ddy,twoxy); setcolor(4); line(twoxy[0],twoxy[1],twoxy[2],twoxy[3]); setcolor(13); line(dx,dy+30,dx,dy-30); line(dx-30,dy,dx+30,dy); q=0; i=0; } } } } /*发射箭,speed1和speed2控制速度,返回弹位置*/ int Launch(int lx,int ly,int tx,int ty,int hm,int grade) { double speed1=0.01; int speed2=1000; int a[4]; int xx[2],xy[2]; double s=0.0,angle=0.0,t=0.0; lx=lx;ly=ly-50; if(hm==3) { a[0]=lx; a[1]=ly; a[2]=tx; a[3]=ty; Fire (a,&s,&angle;,grade); } else { TwoPoints(a,lx,ly); DistanceAngle(a,&s,&angle;); s=s/AMD1; } RelativePosition(&xx;[0],&xy;[0],s,angle,t-1); RelativePosition(&xx;[1],&xy;[1],s,angle,t); for(t=0.0;ly-xy[1]<480;t=t+speed1) { RelativePosition(&xx;[0],&xy;[0],s,angle,t-1); RelativePosition(&xx;[1],&xy;[1],s,angle,t); if(PointPlace(lx+xx[1],ly-xy[1],tx,ty-40,tx+10,ty-30)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx,ty-40,tx-10,ty-30)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx-10,ty-30,tx+10,ty-30)==1) { sound(4000);/*响声函数*/ delay(10000); nosound(); return 1; } if(PointPlace(lx+xx[1],ly-xy[1],tx-5,ty-30,tx+5,ty-30)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx-5,ty-10,tx+5,ty-10)==1&&PointPlace;(lx+xx[1],ly-xy[1],tx-5,ty-30,tx-5,ty-10)==4&&PointPlace;(lx+xx[1],ly-xy[1],tx+5,ty-30,tx+5,ty-10)==3) { sound(3000);/*响声函数*/ delay(10000); nosound(); return 2; } if(PointPlace(lx+xx[1],ly-xy[1],tx-20,ty-10,tx-20,ty+10)==4&&PointPlace;(lx+xx[1],ly-xy[1],tx+20,ty-10,tx+20,ty+10)==3&&PointPlace;(lx+xx[1],ly-xy[1],tx-20,ty-10,tx+20,ty-10)==2&&PointPlace;(lx+xx[1],ly-xy[1],tx-20,ty+10,tx+20,ty+10)==1) { sound(2000);/*响声函数*/ delay(10000); nosound(); return 3; } if(ly-xy[1]<1) { delay(speed2); continue; } if(lx+xx[1]<1||lx+xx[1]>640-1) { return 0; } PictureBullets (lx+xx[0],ly-xy[0],lx+xx[1],ly-xy[1]); delay(speed2); PictureBullets (lx+xx[0],ly-xy[0],lx+xx[1],ly-xy[1]); } return 0; } /*小屋移动的函数*/ int MoveRen(struct Ren *p) { int a,k=19200,b=0,d; int q=1; randomize(); for(;q;) { if(b==1) { p->lr=3; } RenPicture(p->x,p->y,p->color); if(p->lr==3) { b=1; delay(10000); delay(10000); delay(10000); delay(10000); delay(10000); delay(10000); //sleep(1); d=random(10); if(d==0) k=19200; if(d==1) k=19712; if(d==2) k=18432; if(d==3) k=20480; if(d==4) k=7181; p->lr=1; } else { k=bioskey(0); } RenPicture(p->x,p->y,p->color); switch(k){ case 19200: /*按向左键*/ a=(p->x)-5; if(p->lr==1) { if(a>L1&&a<R1) { p->x=a; break; } } else { if(a>640-R1&&a<640-L1) { p->x=a; break; } } break; case 19712: /*按向右键*/ a=(p->x)+5; if(p->lr==1) { if(a>L1&&a<R1) { p->x=a; break; } } else { if(a>640-R1&&a<640-L1) { p->x=a; break; } } break; case 18432: /*按向上键*/ a=(p->y)-5; if(p->lr==1) { if(a>T1&&a<B1) { p->y=a; break; } } else { if(a>T1&&a<B1) { p->y=a; break; } } break; case 20480: /*按向下键*/ a=(p->y)+5; if(a>T1&&a<B1) { p->y=a; } break; case 7181: /*enter键的扫描码*/ if(b==1) p->lr=3; q=0; break; case 283: return 0; } } RenPicture(p->x,p->y,p->color); return 1; } /*游戏开始前画面*/ int GameStar()/*返回1表示单人游戏初级,2表示单人游戏级,3表示单人游戏高级,4表示两人对战,5表示退出游戏*/ { int q,k,h=0; for(;1;) { q=1; cleardevice();/*清屏函数*/ setcolor(15); settextstyle(0,0,5); outtextxy(100,100,"Start Game!"); settextstyle(0,0,1); outtextxy(20,300,"keys used:"); outtextxy(20,300," Arrow keys"); outtextxy(20,310," The left mouse button"); outtextxy(20,320," Enter"); outtextxy(20,330," Esc to Quit!"); setcolor(5); outtextxy(250,400,"One player!"); outtextxy(250,420,"Two players!"); outtextxy(250,440,"Quit!"); setwritemode(1); setcolor(6); rectangle(245,395+h*20,345,415+h*20); for(;q;) { setcolor(6); k=bioskey(0); sound(1000);/*响声函数*/ delay(10000); nosound(); if(k==20480) { rectangle(245,395+h*20,345,415+h*20); h=(h+1)%3; rectangle(245,395+h*20,345,415+h*20); }else if(k==7181) { if(h==0)/*单人游戏,选择等级*/ { cleardevice();/*清屏函数*/ setcolor(2); outtextxy(20,30," Esc to back!"); outtextxy(250,240,"Lower"); outtextxy(250,260,"Middle"); outtextxy(250,280,"Higher"); setcolor(4); rectangle(245,235+h*20,300,255+h*20); for(;q;) { k=bioskey(0); sound(1000);/*响声函数*/ delay(10000); nosound(); if(k==20480) { rectangle(245,235+h*20,300,255+h*20); h=(h+1)%3; rectangle(245,235+h*20,300,255+h*20); }else if(k==7181) { return h+1; }else if(k==283) { h=0; k=1; q=0; break; }else{} } } if(h==1)/*两人对抗*/ return 4; if(h==2)/*退出游戏*/ return 5; }else if(k==283) { return 5; }else {} } } } /*退出游戏画面*/ void GameOver() { cleardevice();/*清屏函数*/ setcolor(14); settextstyle(0,0,6); outtextxy(100,200,"Game Over!"); settextstyle(1,0,1); outtextxy(400,400,"Producer:ChenChen"); outtextxy(400,410," QQ:804620957"); outtextxy(400,420," Time:2010.5.28"); } /*主函数*/ void main() { int gd=DETECT,gm; int q=0,schoose=1; int out=1; int pmc=1; int cla2s=1; struct Ren ren1,ren2; initgraph(&gd;,&gm;,""); /* registerbgidriver(EGAVGA_driver);*/ cleardevice();/*清屏函数*/ MouseMath();/*计算鼠标形状,一开始必须使用,后面就不用了*/ MouseSetY(0,479); MouseSetX(0,649); MouseSetXY(100,100); for(;out;) { pmc=GameStar(); cleardevice();/*清屏函数*/ settextstyle(1,0,1);/*初始化*/ schoose=1; ren2.x=540;ren2.y=320;ren2.life=200;ren2.color=3;ren2.lr=2; if(pmc<4) { ren1.x=640-ren2.x;ren1.y=ren2.y;ren1.life=ren2.life;ren1.color=2;ren1.lr=3; cla2s=pmc; }else if(pmc==4) { ren1.x=640-ren2.x;ren1.y=ren2.y;ren1.life=ren2.life;ren1.color=2;ren1.lr=1; }else { break;} Desktop(); AngleSpeed(0,0); RenPicture(ren1.x,ren1.y,ren1.color); RenPicture(ren2.x,ren2.y,ren2.color); LifePicture(ren1.life,ren1.color,ren1.lr); LifePicture(ren2.life,ren2.color,ren2.lr); for(;ren1.life>0&&ren2;.life>0;schoose++) { if(schoose%2) { RenPicture(ren1.x,ren1.y,ren1.color); if(MoveRen(&ren1;)==0) break; q=Launch(ren1.x,ren1.y,ren2.x,ren2.y,ren1.lr,cla2s); if(q==1) ren2.life=ren2.life-40; if(q==2) ren2.life=ren2.life-20; if(q==3) ren2.life=ren2.life-10; if(ren2.life<0) ren2.life=0; LifePicture(ren2.life,ren2.color,ren2.lr); } else { RenPicture(ren2.x,ren2.y,ren2.color); if(MoveRen(&ren2;)==0) break; q=Launch(ren2.x,ren2.y,ren1.x,ren1.y,ren2.lr,cla2s); if(q==1) ren1.life=ren1.life-40; if(q==2) ren1.life=ren1.life-20; if(q==3) ren1.life=ren1.life-10; if(ren1.life<0) ren1.life=0; LifePicture(ren1.life,ren1.color,ren1.lr); } } if(ren1.life<ren2.life) { settextstyle(0,0,6); setcolor(ren2.color); outtextxy(150,280,"P2 win!"); settextstyle(1,0,1); } else if(ren1.life>ren2.life) { settextstyle(0,0,6); setcolor(ren1.color); outtextxy(150,280,"P1 win!"); settextstyle(1,0,1); } else { settextstyle(0,0,6); setcolor(15); outtextxy(150,280,"Drew!"); settextstyle(1,0,1); } getch(); } GameOver(); getch(); closegraph(); }
一、下载须知: .................本书无目录 .................本书经过内容识别处理,所以好处就是书上的案例源码可以直接粘贴复制到编辑器,当然有个别括号什么的可能需要自己纠正一下。 ................本书高清 ...............本书是和代码结合在一起讲解的,其也讲到了运行时类的识别等等。 二、截取部分章节 《第18章:运行时类型识别》 RTTI的两种使用方法 使用RT T I有两种不同的方法。第一种就像s i z e o f ( ),因为它看上就像一个函数。但实际上它是 由编译器实现的。t y p e i d ( )带有一个参数,它可以是一个对象引用或指针,返回全局t y p e i n f o类的 常量对象的一个引用。可以用运算符“= =”和“!=”来互相比较这些对象。也可以用n a m e ( )来 获得类型的名称。注意,如果给t y p e i d ( )传递一个s h a p e *型参数,它会认为类型为s h a p e *,所以如 果想知道一个指针所指对象的精确类型,我们必须逆向引用这个指针。比如,s是个s h a p e * , cout << typeid(*s).name()<<endl; 将显示出s所指向的对象类型。 也可以用b e f o r e ( t y p e i n f o & )查询一个t y p e i n f o对象是否在另一个t y p e i n f o对象的前面(以定 义实现的排列顺序),它将返回t r u e或f a l s e。如果写: if(typeid(me).before(typeid(you))) //... 那么表示我们正在查询m e在排列顺序是否在y o u之前。 RT T I的第二个用法叫“安全类型向下映射”。之所以用“向下映射”这个词也是由于类继 承的排列顺序。如果映射一个c i r c l e *到s h a p e *叫向上映射的话,那么将一个s h a p e *映射成一个 c i r c l e *就叫向下映射了。当然一个c i r c l e *也是一个s h a p e *,编译器允许任意的向上映射,但一 个s h a p e *不一定就是c i r c l e *,所以编译器在没有明确的类型映射时并不允许我们完成一个向下 映射任务。当然可以用原有的C风格的类型映射或C + +的静态映射(s t a t i c c a s t ,将在本章末介绍) 来强制执行,这等于在说:“我希望它实际上是一个c i r c l e *,而且我打算要求它是。”由于并没 有明确地知道它实际上是c i r c l e,因此这样做是很危险的。在开发商制定的RT T I一般的方法 是:创建一个函数来试着将s h a p e *指派为一个c i r c l e * (在本例),检查执行过程的数据类型。 如果这个函数返回一个地址,则成功;如果返回n u l l,说明我们并没有一个c i r c l e *对象。 C + +的RT T I的“安全类型向下映射”就是按照这种“试探映射”函数的格式,但它(非常 合理地)用模板语法来产生这个特殊的动态映射函数( d y n a m i c c a s t),所以本例变成: 动态映射的模板参数是我们想要该函数创建的数据类型,也就是这个函数的返回值。函数 参数是我们试图映射的源数据类型。 通常只要对一种类型作这种工作(比如将三角型变成紫色),但如果想算出各种s h a p e的数 目,可以用面下例子的框架: 当然这是人为的—我们可能已经在各个类型放了一个静态数据成员并在构造函数对 第18章运行时类型识别383 下载 shape* sp = new circle; circle* cp = dynamic_cast<circle*>(sp); if(cp) cou七<< "cas 七successful"; circle* cp = dynamic_cas区circle*>(sh); square* sp = dynamic_cast<square*>(sh); triangle* 七p = dynamic_cast<triangle*>(sh); 它自增。如果可以控制类的源代码并可以修改它,当然可以这样做。下面这个例子用来计算 s h a p e的个数,它用了静态数据成员和动态映射两种方法: 384 C + +编程思想 下载 //: RTSHAPES.CPP -- Counting shapes #include <iostream.h> #include< 七ime.h> #include <typeinfo.h> #include"·.\14\tstash.h" class shape { protected: S 七atic in七coun店 public: shape() { count++; vir七ual -shape() = O { count--; } vir七ual void draw () const = O; static in七quan七让y() { return count; }; in七shape : : count = O; class rectangle : public shape void operator=(rectangle&); // Disallow protected: static int count; public: rectangle() { count++; rectangle(const rectangle&) { count++;} -rectangle() { count--; } void draw () cons 七{ cout << "rec 七angle: : draw ()" << endl; S 七atic int quantity() { return count; } ... ' int rectangle: : count = 0; class ellipse : public shape void operator=(ellipse&}; // Disallow protected: static int count; public: ellipse () { count++; } ellipse (const ellipse&) { count++; } ~ellipse() { count 一;} void draw() cons 七{ cout << "ellipse: :draw()" << endl; 第18章运行时类型识别385 下载 } static int quantity() { return count; } }; int ellipse::count = O; class circle: public ellipse { void operator=(circle&); // Disallow protected: static int count; public: circle() { count++; } circle(cons 七circle&) { count++; } 一circle() { count--; } void draw() const { cout << "circle: :draw()" << endl; } static in七quantity() { return count; } } ; int circle::count = 0; main() { 七stash<shape> shapes; time_t t; II Seed random number generator: srand((unsigned)time(&t)); const mod= 12; for(int i = 0; i < rand() 令mod; i++) shapes.add(new rectangle); for(int j = O; j < rand() % mod; j++) shapes.add(new ellipse); for(int k = O; k < rand() 兮mod; k++) shapes.add(new circle); int Ncircles = O; int Nellipses = O; int Nrects = O; int Nshapes = O; for(int u = O; u < shapes.count(); u++) { shapes[u]->draw(); if(dynamic_cast<circle*>(shapes[u])) Ncircles++; if(dynamic_cast<ellipse*>(shapes[u])) Nellipses++; if(dynamic_cast<rectangle*>(shapes[u])) Nrects++; 对于这个例子,两种方法都是可行的,但静态数据成员方法只能用于我们拥有源代码并已 安装了静态数据成员和成员函数时(或者开发商已为我们提供了这些),另外RT T I可能在不同 的类用法不同。 18.3 语法细节 本节详细介绍RT T I的两种形式是如何运行的以及两者之间的不同。 18.3.1 对于内部类型的typeid() 为了保持一致性, t y p e i d ( )也可以运用于内部类型,所以下面的表达式结果为t r u e: 18.3.2 产生合适的类型名字 t y p e i d ( )必须在所有的状况下都可以运行,比方说,下面的类包含了一个嵌套类: 386 C + +编程思想 下载 } if(dynamic_cast<shape*>(shapes[u])) Nshapes++; cout << endl << endl <<"circles=• << Ncircles << endl <<"ellipses=• << Nellipses << endl << "rec 七angles="<< Nrects << endl <<"shapes="<< Nshapes << endl << endl << "circle: :quantity() =• << circle: :quantity() << endl << "ellipse: :quantity() = " << ellipse: :quantity() << endl << "rectangle: :quantity() =" << rectangle: : quantity () << endl << "shape: :quantity() =• << shape: :quantity() << endl;

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值