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的更多相关文章
随机推荐
- 【LeetCode每天一题】Plus One(加一)
Given a non-empty array of digits representing a non-negative integer, plus one to the integer.The d ...
- AIX修改密码
grid@DB01:/home/grid>su - oracleoracle's Password: 3004-303 There have been too many unsuccessful ...
- 69.js--点击事件等比例弹出层div
html:<!--弹出层导航栏--> <div class="public-nav-content"> <ul> <li><a ...
- PHP字符串格式化特点和漏洞利用点
转载至: https://www.anquanke.com/post/id/170850 PHP中的格式化字符串函数 在PHP中存在多个字符串格式化函数,分别是printf().sprintf().v ...
- EF:分页查询 + 条件查询 + 排序
/// <summary> /// linq扩展类---zxh /// </summary> /// <typeparam name="T">& ...
- 梳理vue双向绑定的实现原理
Vue 采用数据劫持结合发布者-订阅者模式的方式来实现数据的响应式,通过Object.defineProperty来劫持数据的setter,getter,在数据变动时发布消息给订阅者,订阅者收到消息后 ...
- [经验共享] MapGIS实用小功能图解——由excel文件导成MapGIS点文件
项目小组的几个成员都是学地下水和环境的,对于GIS懂得不是很多,于是把一些我们经常用到的mapgis实用小功能做成帮助文档,方便大家使用,发布共享! 1.整理好EXCEL文件(注意X,Y坐标的正确性( ...
- [批处理] Git中log的使用
1.获取两个提交之间的日志: git log SHA-1_A.. SHA-1_B--pretty=format:"%cd: %s" --date=format:%Y%m%d > ...
- 移动App专项测试
移动App测试实战—专项测试 转自:http://www.51testing.com/html/58/n-3713758.html 我们在进行了手工的功能测试之后,也开发了一些自动化测试用例,并且做了 ...
- 菜鸟redis初学
该随笔为本人自学redis所遇到的错误,写这些初衷完全是为了避免以后犯相同的错误,如果对别人有帮助,那就相互促进. 在Java中使用redis,首先你的Jdk要能运行,如果没配置好,网上有很多jdk环 ...