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. 牛客随笔(c++)

    1.关于指针的字节大小: 当为32位系统时大小为4字节,64位系统时大小为8字节: #include<iostream> using namespace std; int main() { ...

  2. 使用Apache JMeter对SQL Server、Mysql、Oracle压力测试(二)

    接着第一篇的写: 第三步,测试SQL Server数据库的性能: a.加载JDBC SQL Server驱动.添加线程组和设置线程属性和第二步一样,就不再赘述了: b.设置JDBC Connectio ...

  3. 用HTML5+原生js实现的推箱子游戏

    <!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...

  4. H.264学习--1

    1.宏块(Macro Block):一个编码图像首先要划分成多个块(4x4 像素)才能进行处理,显然宏块应该是整数个块组成,通常宏块大小为                               ...

  5. listener 监听 tomcat 容器的初始化和销毁

    为了简单,就写个统计Action 请求数量的例子: 1.首先写个 listener public class TestServletContextListener implements Servlet ...

  6. Java获取项目根目录等其他系统属性

    一 相对路径的获得 说明:相对路径(即不写明时候到底相对谁)均可通过以下方式获得(不论是一般的java项目还是web项目) String relativelyPath=System.getProper ...

  7. BeyondCompare使用一段时间后会因“许可证密钥已被撤销:3281-0350“而无法使用

    解决方式: 1.用任意文本编辑软件打开“C:\Users\[Your User Name]\AppData\Roaming\Scooter Software\Beyond Compare 3\BCSt ...

  8. Android-Gradle(四)

    当你在开发一个app,通常你会有几个版本.大多数情况是你需要一个开发版本,用来测试app和弄清它的质量,然后还需要一个生产版本.这些版本通常有不同的设置,例如不同的URL地址.更可能的是你可能需要一个 ...

  9. tcp ip 协议安全

    ARP(地址解析协议) 地址解析协议,即ARP(Address Resolution Protocol),是根据IP地址获取物理地址的一个TCP/IP协议.主机发送信息时将包含目标IP地址的ARP请求 ...

  10. Lua table遍历

    工作中,栽了一个“坑”,特此备录. [1]遍历table1,每次结果可能都不同 -- 获取value ", addr="xian"} for k, v in pairs( ...