dynamic_cast

static_cast

reinterpret_cast

 #include <iostream>

 using namespace std;

 class A{
public:
//动态类型转换只能用于多态继承
virtual void foo(void){}
}; class B: public A{};
class C: public B{};
class D{}; int main(void)
{
B b;
A* pa = &b; //B is a A --皆然性
cout << "pa= " << pa << endl;
cout << "动态类型装换" << endl;
//pa实际指向B类对象,转换成功
B* pb = dynamic_cast<B*> (pa);
cout << "pb= " << pb << endl;
//pa没有指向C类对象,转换失败,安全
C* pc = dynamic_cast<C*> (pa);
cout << "pc= " << pc << endl;
try{
A& ra = b;
C& rc = dynamic_cast<C&> (ra);
}
catch(exception& ex){
cout << ex.what() << endl;
}
//pa没有指向D类对象,转换失败,安全
D* pd = dynamic_cast<D*> (pa);
cout << "pd= " << pd << endl;
cout << "静态类型转换" << endl;
//B是A的子类,转换成功
pb = static_cast<B*> (pa);
cout << "pb= " << pb << endl;
//C是A的孙子类,转换成功,危险
pc = static_cast<C*> (pa);
cout << "pc= " << pc << endl;
//D和A没有继承关系,转换失败,安全
//pd = static_cast<D*> (pa);
//cout << "pd= " << pd << endl;
cout << "重解释类型转换" << endl;
//编译期、运行期均不做检查,永远成功,最危险
pb = reinterpret_cast<B*> (pa);
cout << "pb= " << pb << endl;
pc = reinterpret_cast<C*> (pa);
cout << "pc= " << pc << endl;
pd = reinterpret_cast<D*> (pa);
cout << "pd= " << pd << endl; return ;
}

5.typeid.cpp

 #include <iostream>
#include <typeinfo> using namespace std; namespace ns1{
class A{
public:
class B{
public:
class C{};
};
};
}
class Base{
//virtual void foo(void){};
};
class Derived: public Base{}; //class A{}; int main(void)
{
int a = ;
cout << typeid(int).name() << endl; //i
cout << typeid(unsigned int).name() << endl; //j
cout << typeid(char).name() << endl; //c
cout << typeid(unsigned char).name() << endl; //h
cout << typeid(short).name() << endl; //s
cout << typeid(unsigned short).name() << endl; //t
cout << typeid(long).name() << endl; //l
cout << typeid(long long).name() << endl; //x
cout << typeid(float).name() << endl; //f
cout << typeid(double).name() << endl; //d
cout << typeid(void).name() << endl; //v
cout << typeid(bool).name() << endl; //b
cout << typeid(int*).name() << endl; //Pi
cout << typeid(int**).name() << endl; //PPi
cout << typeid(int&).name() << endl; //i
cout << typeid(float[]).name() << endl; //A4_f
cout << typeid(char*[]).name() << endl; //A4_Pc
cout << typeid(char(*)[]).name() << endl; //PA4_c
cout << typeid(short[][][]).name() << endl; //A2_A3_A4_s
cout << typeid(char*(*)(short*, int*)).name() << endl; //PFPcPsPiE struct Student{
char name[];
int age;
};
cout << typeid(Student).name() << endl; //Z4mainE7Student
cout << typeid(ns1::A::B::C).name() << endl; //N3ns11A1B1CE Derived d;
Base* p = &d;
//基类中无虚函数
//cout << typeid(*p).name() << endl; //4Base
//Base& r = d;
//cout << typeid(r).name() << endl; //4Base
//基类中有虚函数:virtual void foo(void){};
cout << typeid(*p).name() << endl; //7Derived
Base& r = d;
cout << typeid(r).name() << endl; return ;
}

6.shapes.cpp

 #include <iostream>
#include <typeinfo> using namespace std; //形状:位置、绘制
//+--圆形:半径、(绘制)
//+--矩形:长宽、(绘制)
//形状
class Shape{
public:
Shape(int x, int y): m_x(x), m_y(y){}
virtual void dummy(void){};
void draw(void) const{}; //纯虚函数 protected:
int m_x;
int m_y;
}; //圆形
class Circle: public Shape{
public:
Circle(int x, int y, int r): Shape(x, y), m_r(r){}
void draw(void) const
{
cout << "圆形(" << m_x << ',' << m_y << ',' << m_r << ')' << endl;
}
private:
int m_r;
}; //矩形
class Rectangle: public Shape{
public:
Rectangle(int x, int y, int w, int h): Shape(x, y), m_w(w), m_h(h){}
void draw(void) const
{
cout << "矩形(" << m_x << ',' << m_y << ',' << m_w << ',' << m_h << ')' << endl;
}
private:
int m_w;
int m_h;
}; void render(Shape* shapes[])
{
for(size_t i = ; shapes[i]; ++i)
if(typeid(*shapes[i]) == typeid(Circle))
static_cast<Circle*> (shapes[i])->draw();
else
if(typeid(*shapes[i]) == typeid(Rectangle))
static_cast<Rectangle*> (shapes[i])->draw();
} void drawAny(Shape const& shape)
{
shape.draw();
} int main(void)
{
Shape* shapes[] = {};
shapes[] = new Circle (,,);
shapes[] = new Circle(,,);
shapes[] = new Rectangle(,,,);
shapes[] = new Rectangle(,,,);
shapes[] = new Circle(,,);
render(shapes); return ;
}

7.dec.cpp

 #include <iostream>

 using namespace std;

 class Base{
public:
Base(void):m_array(new int[])
{
cout << "基类构造" << endl;
}
virtual ~Base(void)
{
cout <<"基类析构" <<endl;
delete[] m_array;
}
private:
int* m_array;
}; class Derived: public Base{
public:
Derived(void):m_buffer(new char[])
{
cout << "子类构造" << endl;
}
~Derived(void)
{
cout << "子类析构" << endl;
delete [] m_buffer;
}
private:
char* m_buffer;
}; int main(void)
{
Base* pb = new Derived;
delete pb; //在没有虚析构的情况下:基类函数不会调子类的析构函数
//使用虚析构后:调用的是子类的析构函数,该析构函数一方面析构子类特有的资源
//,另一方面还会自动调用基类的析构函数,最后连基类带子类所有的
//资源全部析构干净。 return ;
}
//任何时候将基类的析构函数声明为虚函数,总不会有坏处

虚析构:

任何时候将基类的析构函数声明为虚函数,总不会有坏处

C++_day9am的更多相关文章

随机推荐

  1. DS1-14

    #include <stdio.h> #define MAXSIZE 10000 int MaxSubseqSum4(int List[], int N); int main() { in ...

  2. vi命令删除

    3.删除 x :删除当前光标位置的字符 X    :删除当前光标位置前的字符 dd :删除当前行

  3. Extjs6 modern formpanel 上传文件 问题

    要设置 enableSubmissionForm: false 否则chrome会报 Form submission canceled because the form is not connecte ...

  4. Linux学习路线全解,Linux操作系统学习路线

    大家都知道,在现在这个信息化飞速发展的时代,IT技术火速发展,信息的重要性,可想而知.现在,在北京当一个高级运维工程师,年薪百万已经不是梦想.当然我也想,谁不想挣大钱,开好车,住好房.下面说说自己的一 ...

  5. 接口自动化框架(java)--4.接口Token传递

    这套框架的报告是自己封装的 一般token会在登录接口返回结果中呈现,从代码层面获取token的方式有很多种,我是使用jsonpath这个json路径语言去匹配token所在路径的key值 packa ...

  6. nginx----------前端写了一套带有vue路由的的功能。放到nginx配置的目录下以后,刷新会报404未找到。

    1. 这是根据实际情况来写的. location /h5/activity/wechat/ {            index  index.html index.htm index.php;    ...

  7. 安装mysql时出现应用程序无法正常启动(0xc000007b)

    在重装mysql数据库(解压版)遇到了无法正常启动(0xc000007b)问题解决方案 问题描述: 在cmd控制台,mysql的安装路径下使用mysql install命令出现以下无法正常启动(0xc ...

  8. js闭包讲解

    今日看到之前写的一段js代码,关于导航部分鼠标放上去变类,鼠标离开等效果 前端代码 <div class="con12"> <div class="le ...

  9. urlconnection代码

    package com.yucheng.connection; import java.io.BufferedReader; import java.io.InputStream; import ja ...

  10. 转--Python re模块 验证11位手机号

      一.常用正则表达式符号和语法:   '.' 匹配所有字符串,除\n以外 ‘-’ 表示范围[0-9] '*' 匹配前面的子表达式零次或多次.要匹配 * 字符,请使用 \*. '+' 匹配前面的子表达 ...