程序清单 15.17 rtti1.cpp
#include<iostream>
#include<cstdlib>
#include<ctime>
using std::cout;
class Grand{
private:
int hold;
public:
Grand(int h=0):hold(h){}
virtual void Speak()const{cout<<"I am a grand class!\n";}
virtual int Value()const{return hold;}
};
class Superb:public Grand{
public:
Superb(int h=0):Grand(h){}
void Speak()const{cout<<"I am a superb class!\n";}
virtual void Say()const{
cout<<"I hold ther superb value of "<<Value()<<"\n";
}
};
class Magnificent:public Superb{
private:
char ch;
public:
Magnificent(int h=0,char c='A'):Superb(h),ch(c){}
void Speak()const{cout<<"I am a Magnificent class!\n";}
void Say()const{
cout<<"I hold the character "<<ch<<" and the integer"
<<Value()<<"!\n";
}
};
Grand * GetOne();
int main(){
std::srand(std::time(0));
Grand * pg;
Superb* ps;
for(int i=0;i<5;i++)
{
pg=GetOne();
pg->Speak();
if(ps=dynamic_cast<Superb*>(pg)){
ps->Say();
}
}
std::cin.get();
return 0;
}
Grand * GetOne()
{
Grand *p;
switch(std::rand()%3)
{
case 0:p=new Grand(std::rand()%100);break;
case 1:p=new Superb(std::rand()%100);break;
case 2:p=new Magnificent(std::rand()%100,'A'+std::rand()%26);break;
}
return p;
}
程序清单 15.18 rtti2.cpp
#include<typeinfo>
#include<iostream>
#include<cstdlib>
#include<ctime>
using std::cout;
class Grand{
private:
int hold;
public:
Grand(int h=0):hold(h){}
virtual void Speak()const{cout<<"I am a grand class!\n";}
virtual int Value()const{return hold;}
};
class Superb:public Grand{
public:
Superb(int h=0):Grand(h){}
void Speak()const{cout<<"I am a superb class!\n";}
virtual void Say()const
{
cout<<"I hold ther superb value of "<<Value()<<"\n";
}
};
class Magnificent:public Superb{
private:
char ch;
public:
Magnificent(int h=0,char c='A'):Superb(h),ch(c){}
void Speak()const{cout<<"I am a Magnificent class!\n";}
void Say()const
{
cout<<"I hold the character "<<ch<<" and the integer"
<<Value()<<"!\n";
}
};
Grand * GetOne();
int main(){
std::srand(std::time(0));
Grand * pg;
Superb* ps;
for(int i=0;i<5;i++)
{
pg=GetOne();
cout<<"Now proeccsing type "<<typeid(*pg).name()<<".\n";
pg->Speak();
if(ps=dynamic_cast<Superb*>(pg))
ps->Say();
if(typeid(Magnificent)==typeid(*pg))
cout<<"Yes,you're really magnificent.\n";
}
std::cin.get();
return 0;
}
Grand * GetOne()
{
Grand *p;
switch(std::rand()%3)
{
case 0:p=new Grand(std::rand()%100);break;
case 1:p=new Superb(std::rand()%100);break;
case 2:p=new Magnificent(std::rand()%100,'A'+std::rand()%26);break;
}
return p;
}
程序清单 15.18 rtti2.cpp补充
#include<typeinfo>
#include<iostream>
#include<cstdlib>
#include<ctime>
using std::cout;
class Grand{
private:
int hold;
public:
Grand(int h=0):hold(h){}
virtual void Speak()const{cout<<"I am a grand class!\n";}
virtual int Value()const{return hold;}
};
class Superb:public Grand{
public:
Superb(int h=0):Grand(h){}
void Speak()const{cout<<"I am a superb class!\n";}
virtual void Say()const
{
cout<<"I hold ther superb value of "<<Value()<<"\n";
}
};
class Magnificent:public Superb{
private:
char ch;
public:
Magnificent(int h=0,char c='A'):Superb(h),ch(c){}
void Speak()const{cout<<"I am a Magnificent class!\n";}
void Say()const
{
cout<<"I hold the character "<<ch<<" and the integer"
<<Value()<<"!\n";
}
};
Grand * GetOne();
int main(){
std::srand(std::time(0));
Grand * pg;
Superb* ps;
Magnificent * pm;
for(int i=0;i<5;i++)
{
pg=GetOne();
if(typeid(Magnificent)==typeid(*pg))
{
pm=(Magnificent *)pg;
pm->Speak();
pm->Say();
}else if(typeid(Superb)==typeid(*pg))
{
ps=(Superb*)pg;
ps->Speak();
ps->Say();
}else
{
pg->Speak();
}
}
std::cin.get();
return 0;
}
Grand * GetOne()
{
Grand *p;
switch(std::rand()%3)
{
case 0:p=new Grand(std::rand()%100);break;
case 1:p=new Superb(std::rand()%100);break;
case 2:p=new Magnificent(std::rand()%100,'A'+std::rand()%26);break;
}
return p;
}
15.5 类型转换运算符
C语言通用类型转换运算符
#include<stdio.h>
struct Data
{
double data[200];
};
struct Junk
{
int junk[100];
};
int main()
{
int n;
struct Data d={2.5e33,3.5e-19,20.2e32};
char * pch=(char*)(&d);
char ch=(char)&d;
struct Junk* pj=(struct Junk *)&d;
printf("%f\n",d.data[0]);
printf("%s\n",pch);
printf("%c\n",ch);
printf("%d\n",pj->junk[0]);
scanf("%d",&n);
return 0;
}
程序清单 15.19 constcast.cpp
#include<iostream>
using std::cout;
using std::endl;
void change(const int * pt,int n);
int main()
{
int pop1=38383;
const int pop2=2000;
cout<<"pop1,pop2:"<<pop1<<", "<<pop2<<endl;
change(&pop1,-103);
change(&pop2,-103);
cout<<"pop1,pop2:"<<pop1<<", "<<pop2<<endl;
std::cin.get();
return 0;
}
void change(const int * pt,int n)
{
int *pc;
pc=const_cast<int *>(pt);
*pc+=n;
}