C++_day9am

时间:2022-11-08 12:47:07

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 ;
}

C++_day9am

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. Dtrace for Linux 2016

      http://www.brendangregg.com/blog/2016-10-27/dtrace-for-linux-2016.html

    2. Wheel ProgressBar 实现之三——模拟进度过程

      1. 效果展示: 知道如何画圆弧,如何精确画出进度文本之后,我们将进入 Wheel ProgressBar 实现的最后一个过程:模拟其动态呈现过程.如下图所示,初始时显示进度为 0 (上图),点击进度 ...

    3. 简析Android 兼容性测试框架CTS使用

      一.什么是兼容性测试? 1)为用户提供最好的用户体验,让更多高质量的APP可以顺利的运行在此平台上 2)让程序员能为此平台写更多的高质量的应用程序 3)可以更好的利用Android应用市场 二.CTS ...

    4. ArcSDE10&period;1配置Oracle 监听器来使用SQL操作ST&lowbar;Geometry&lpar;个人改动版&rpar;

      发了两天的时间来解决配置Oracle 监听器来使用SQL操作ST_Geometry的配置,网上搜索一大片,结果真正找到的只有方法可用,下面把这个方法我个人在总结下. ArcSDE10.1配置Oracl ...

    5. 【OSG】将显示的图形窗口化

      窗口化原理 有时为了方便控制场景渲染,需要设置一个合适的图形环境窗口(即窗口化). 创建图形环境的主要步骤如下: (1)通过WindowingSystemInterface类得到系统窗口接口,该系统接 ...

    6. powerdesigner 设置字段显示comment注释

      在Columns标签下,一排按钮中找到这个按钮:Customize Columns and Filter

    7. window 杀死已开任务启命令

      1. 查询已开启的端口号 C:\Users\Administrator>netstat -ano | findstr 如果被占用会查询出相关信息,如果没有被占用则不会输出任何信息,查询到已开启信 ...

    8. oracle 安装,启动 ,plsql 连接

      1.下载oracle 服务器端,正常安装,在选择桌面类或者是服务器类的时候选择服务器类. 2.下载oracle 客户端解压版 下载地址   链接:https://pan.baidu.com/s/1mi ...

    9. 洛谷 P2090 数字对

      发现如果给定两个数(a,b),可以用类似辗转相除法在logn的时间内计算出(反向)变到(1,1)的最小步数. 然而并不知道另一个数是多少? 暴力嘛,枚举一下另一个数,反正1000000的nlogn不虚 ...

    10. ORACLE中的Net Configuration Assistant 点击后无反应&comma; sqlplus登录数据库提示Oracle11g ORA-12560&colon; TNS&colon; 协议适配器错误

      首先是对于点击无反应问题: 如果是客户端下的Net Configuration Assistant可用,而服务器端的Net Configuration Assistant等工具不可用的原因如下. 环境 ...

    相关文章