C++_day9am
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的更多相关文章
随机推荐
- JSP页面、包含
JSP页面概念: html称为静态页面,而与相对的JSP称为动态页面(一个特殊的servlet)二者的区别在于,html只能定义css,js.但是在JSP中除了html可以定义的文件外,还可以定义Ja ...
- 使用TCP通信文件上传
客服端读取本地文件,吧文件上传到服务器,服务器在吧上传的文件保存到服务器硬盘上方法分析1:客户端使用本地字节输入流读取要上传的文件 2:客户端使用网络字节输出流,吧读取到的文件上传到服务器 3:服务器 ...
- CDIE2019中国数字化创新展暨首席信息官峰会上海站来袭~
China Digital Innovation Expo & CIO Summit 2019是由Dot Connector(上海华昂商务咨询有限公司)主办的第五届聚焦中国技术领袖,探索创新, ...
- mysql执行update语句受影响行数是0
mybatis连接mysql数据库,发现同一个update执行多次,返回的int值都是1. 我记得同样的update再次执行时 受影响行数是0. 后来发现,我之前一直用的SQLyog是这样子的. 原来 ...
- 用php实现斐波那契数列,如: 1, 1, 2, 3, 5, 8, 13, 21, 34。用数组求出第20个数的值。
<?php //用数组 function fib($n){ $array = array(); $array[0] = 1; $array[1] = 1; for($i=2;$i<$n;$ ...
- Apache的功能模块
本人这几天一直在看apache相关的书籍,稍微说下apache的结构 本人的制图: Apache一共有五层功能结构. 从底层到上依次为: 第一层: 名称:操作系统支持层 功能:操作系统可以提供底层功能 ...
- meterpreter 持久后门
创建持久后门 当成功获取目标系统的访问权限后,需要寻找方法来恢复与目标主机的连接,而无需再进入目标系统.如果目标用户破坏了该连接,例如重新启动计算机,此时使用后门将允许自动重新与目标系统建立连接.为了 ...
- Ubuntu 安装第三方工具
1. pycharm 安装(链接:https://pan.baidu.com/s/1fIp-AhBmnPvqYW40140RLw 提取码:ukkv ) 1.运行以下命令安装 sh pycha ...
- python,Day1,基础1
主要内容 1.python介绍 2.发展史 3.安装 4.hello world程序 5.变量 6.用户输入 7.模块 8.数据类型 9.数据运算 10.if...else语句 11.while循环 ...
- centos7安装git
1.安装git依赖包 yum install curl-devel expat-devel gettext-devel openssl-devel zlib-devel gcc perl-ExtUti ...