标签:用法 ssd def stp crp mic ocm RKE mlu
#include <iostream>
using namespace std;
class A
{
public:
virtual void disp()
{ cout<<"A::disp()"<<endl; }
void printA()
{ cout<<"A::printA()"<<endl; }
};
class B : public A
{
public:
void disp()
{ cout<<"B::disp()"<<endl; }
void printB()
{ cout<<"B::printB()"<<endl; }
};
int main()
{
A *pa = new B;
B *pb = dynamic_cast<B *>(pa); //转换安全
if(pb != NULL)
{ pb->printB(); }
else
{ cout<<"转换不安全,退出"<<endl; }
A *p1 = new A;
B *p2 = dynamic_cast<B *>(p1); //转换不安全
if(p2 != NULL)
{ p2->printB(); }
else
{ cout<<"转换不安全,退出"<<endl; }
return 0;
}
|
#include <iostream>
#include <typeinfo>
#include <string.h>
using namespace std;
class Animal //抽象类
{
public:
virtual void Say()=0; //纯虚函数
};
class Dog:public Animal
{
public:
void Say()
{ cout<<"汪汪"<<endl; }
};
class Cat:public Animal
{
public:
void Say()
{ cout<<"喵喵"<<endl; }
};
void play(Animal *pa)
{
if(typeid(*pa) == typeid(Dog)) //判断当前指针是不是Dog类型
{
Dog * dog = dynamic_cast<Dog*>(pa);
dog->Say();
}
if(typeid(*pa) == typeid(Cat))
{
Cat * cat = dynamic_cast<Cat*>(pa);
cat->Say();
}
}
|
int main()
{
Dog dog;
Animal *a = &dog;
cout<<typeid(double).name()<<endl; //只输出类型第一个字母
cout<<typeid(5).name()<<endl;
cout<<"typeid(a).name() "<<typeid(a).name()<<endl; //指针类型
cout<<"typeid(*a).name() "<<typeid(*a).name()<<endl;
//指针所存的值类型
play(a);
Cat cat;
play(&cat);
}
|
cout<<typeid(5).name()<<endl; //输出字符串“int”
cout<<typeid(double).name()<<endl; //输出字符串“double”
|
class A{……}; //包含虚函数
class B:pulic A{……}
A* pa=new B;
cout<<typeid(*pa).name()<<endl; //输出结果为B
|
#include <iostream>
#include <typeinfo>
using namespace std;
int main()
{
const int i=5;
const int *pi=&i;
//*pi = 6; //不能更改
cout<<"i= "<<i<<endl;
cout<<"*pi = "<<*pi<<endl;
cout<<"pi = "<<pi<<endl;
int *pi1=const_cast<int *>(pi); //为什么这样?因为const int *不能初始化int *
//去除pi的const属性,返回新类型变量,不会影响i
*pi1 = 6;
//*pi = 6; //不能更改
cout<<"i= "<<i<<endl;
cout<<"*pi = "<<*pi<<endl;
cout<<"pi = "<<pi<<endl;
cout<<"*pi1 = "<<*pi1<<endl;
cout<<"pi1 = "<<pi1<<endl;
*(const_cast<int *>(pi))=6; //前面操作可合并为这个
return 0;
}
|
#include <iostream>
using namespace std;
int main()
{
double d = 2.5;
double *pd = &d;
int *pi = reinterpret_cast<int *>(pd);
//int *pi1 = static_cast<int *>(pd); //error
//int *pi2 = dynamic_cast<int *>(pd); //error
int *pi3 = (int *)(pd);
cout<<"pd= "<<pd<<"\t\tpi= "<<pi<<"\t\tpi3= "<<pi3<<endl; //因为指针类型不同,所以值不同
cout<<"*pi= "<<*pi<<"\t\t*pi3= "<<*pi3<<endl;
int a = 3; //这里必须int
int *a1 = reinterpret_cast<int *>(a); //整形量转换为指针,只能值int类型
double *a2 = reinterpret_cast<double *>(a);
cout<<"a1= "<<a1<<"\t\ta2= "<<a2<<endl;
cout<<"*a1= "<<*a1<<"\t\t*a2= "<<*a2<<endl; //段错误
return 0;
}
|
#include<iostream>
using namespace std;
class point
{
public:
point(int x=0,int y=0):_x(x),_y(y){}
virtual void show()
{
cout<<"("<<_x<<","<<_y;
}
private:
int _x;
int _y;
};
class point3D:public point
{
public:
point3D(int _x=0,int _y=0,int z=0):point(_x,_y),_z(z){}
virtual void show()
{
point::show();
cout<<","<<_z<<")"<<endl;
}
private:
int _z;
};
class String
{
public:
String():_mchar(new char[1]){}
void show()
{
cout<<_mchar<<endl;
}
private:
char* _mchar;
};
|
int main()
{
//下面的转换本来是无意义和非法的,以后使用ps->Show()
//成员函数时可能会引起内存错误或得到错误的值, 但编译却不出错. 留下隐患
point3D p1(1,2,3);
String* sp = (String*)&p1;
sp->show(); // 编译通过,调用直接段错误 // 派生类里面加了point::限定就不会段错误了
//但改成下面使用static_cast形式进行转换, 在编译时就报错, 能及时发现错误
//sp = static_cast<String*> (&p1); // 编译报错
cout<<"----------------------------------------------------------------"<<endl;
//而下面这种转换之所以能编译通过,是因为CPoint和CPoint3D的指针本来就可以相互转换
point* pBase = static_cast<point*> (&p1);
pBase->show();
cout<<endl;
return 0;
}
|
标签:用法 ssd def stp crp mic ocm RKE mlu
原文地址:https://www.cnblogs.com/meihao1203/p/9368287.html