能力查询
2011-05-25 17:01
288 查看
本文内容来源:《C++必知必会》条款27
能力查询用于判断一个对象是否支持一个特定的操作,它是通过对“不相关”的类型进行dynamic_cast转换而表达的,这种dynamic_cast用法通常被称为"cross-cast"。
能力查询用于判断一个对象是否支持一个特定的操作,它是通过对“不相关”的类型进行dynamic_cast转换而表达的,这种dynamic_cast用法通常被称为"cross-cast"。
#include <stdio.h> class Rollable{ public: virtual ~Rollable(){ } virtual void roll() = 0; }; class Shape{ public: virtual ~Shape(){ } virtual void draw() const = 0; }; class Circle: public Shape, public Rollable{ public: void draw() const { printf("A circle is drawing!\r\n"); } void roll(){ printf("A circle is rolling!\r\n"); } }; class Square: public Shape{ public: void draw() const{ printf(" A square is drawing!"); } }; int main(int argc, char **argv) { Shape *pShape = new Circle(); pShape->draw(); Rollable *pRoller = dynamic_cast<Rollable *>(pShape); if(pRoller){ //the Circle class has implemented the Rollable interface, thus pRoller is not NULL pRoller->roll(); } Shape *pShape2 = new Square(); pShape2->draw(); Rollable *pRoller2 = dynamic_cast<Rollable *>(pShape2); if(pRoller2){ //the Square class hasn't implemented the Rollable interface,thus pRoller isNULL pRoller2->roll(); } return 0; }
相关文章推荐
- 给MySQL增加mysql-udf-http和mysql-udf-json自定义函数,让MySQL有调用http接口和查询直接回JSON的能力
- LINQ,.NET语言级集成查询能力(.NET Language Integrated Query)
- C++必知必会之(27)能力查询
- 查询能力(SIP OPTIONS)
- 二次号查询能力,治疗二次号的后遗症
- Hibernate 实现Oracle的中文汉字按照拼音排序 之延续,通用的查询排序能力。
- C++之能力查询
- ZStack云计算架构探秘(五): 超强查询能力
- 查询能力(SIP OPTIONS)
- SIP OPTIONS 查询能力
- LINQ,.NET语言级集成查询能力(.NET Language Integrated Query)
- cuda 计算能力查询表
- dynamic_cast与能力查询
- 网路上查询状况的指令:
- 使用DISTINCT去掉重复,并查询所有字段
- Linux端口查询
- sql 2000 字段查询修正
- android 入门学习笔记 电话簿查询
- 一个MYSQL多值查询的存储过程
- [ExtNet] GridPanel怎么实现服务器端分页、排序、查询?--Oracel存储过程分页