- 浏览: 399005 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (309)
- xaml C# wpf (0)
- scala java inner clas (1)
- Tools UML Eclipse UML2 (1)
- Timer .NET Framework (1)
- perl (6)
- python function paramter (1)
- Python Docstring (1)
- Python how to compare types (1)
- Python (8)
- java (5)
- C# (76)
- C# WPF (0)
- p4 (0)
- WPF (46)
- .net (6)
- xaml (1)
- javascript (40)
- windows (10)
- scala (4)
- winform (1)
- c++ (48)
- tools (12)
- cmd (1)
- os (0)
- CI (0)
- shell (0)
- C (2)
- haskell (49)
- functional (1)
- tool (1)
- gnu (1)
- linux (1)
- kaskell (0)
- svn (0)
- wcf (3)
- android (1)
最新评论
There are several built-in type of cast that is available, they are
- reinterpret_cast
- static_cast
- dynamic_cast
in tihs topic let's take a close look at the dynamic_cast.
why do we need the dynamic_cast.
suppose that we have the following class hierarchy
/** why do we need dynamic_cast * consider the following case. */ class employee { public: virtual int salary(); }; class manager : public employee { public: int salary(); }; class programmer : public employee { public: int salary(); };
What is the most common problem of programming, change, yes, it is. suppose that we will have a bonus method to the employee class and only the programmer class has a meaningful implementation to the bonus method. now the class hierarchy has changed to this.
/* * now we will add more method to the class hierarchy above */ class employee { public: virtual int salary(); virtual int bonus(); }; class manager : public employee { public: int salary(); }; class programmer : public employee { public: int salary(); int bonus(); };
now, to make the code work , we have to tell if employee pointer or referfence is a manager/programmer so that we can do things that is specific to the type of class themselves.
class company { public : void payroll(employee *pe); //void payroll(employee &re) ; }; /** * the * dynamic_cast * operator therefore performans two operational at once, it verifies that the request cast is indeede valid, and then only if it * is valid does it performs the cast. The verfication take place at * compile time * the dynamic_cast is safer than the other c++ cast operations because the other casts do no verify whether the cast can actually be performed. * * the dynamic_cast is used for safe casting from a pointer to base class to a pointer to a derived class, often referred to as safe downcasting. */ void company::payroll(employee *pe) { // programmer *pm = dynamic_cast<programmer *> (pe); // if pe refers to an object of type programmer, // the dynamic_cast is successful, // and pm refers to the start of the programmer object if (pm) { // use pm to call programmer::bonus(); // if pe not refer to an object of type programmer, // the dynamic_cast fails, // an pm has the vlaue 0 } else { // use of employee's member function } }
What is special about dynamic_cast
so what is special about the dynamic_cast itself.
The dynamic_cast operator therefore performans two operational at once, it verifies that the request cast is indeede valid, and then only if it is valid does it performs the cast. The verfication take place at runtime.
the dynamic_cast is safer than the other c++ cast operations because the other casts do no verify whether the cast can actually be performed.
The dynamic_cast is used for safe casting from a pointer to base class to a pointer to a derived class, often referred to as safe downcasting.
dynamic_cast can be lvalue reference
what we have seen in the example are cases with pointer, it is not just the pointer that can be used in the dynamic_cast, the lvalue reference can be supported as well.
see the example below.
/* * dynamic_cast * * can converts a pointer to a base class to a pointer to a derived class. A dynamic cast can also be used to convert an lvalule * of base class to a reference to a derived class the syntax is as follow */ void dynamic_cast_references() { //employee * pe = new employee(); programmer * pp= new programmer(); employee & emp = dynamic_cast<programmer&>(*pp); }
error condition
As we said before that dynamic_cast performs at the runtime, what if the cast fails, what will happens? and how to handle and possibly recover from the error cast condition.
/** * error condition * */ #include <typeinfo> void company::payroll(employee & re) { try { programmer &rm = dynamic_cast<programmer &>( re); } catch (std::bad_cast) { // use of employee member functions } }
发表评论
-
不安装Visual Studio,只用Windows SDK搭建VC环境
2013-12-31 21:52 15338首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 1008RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 769It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 955As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1057typeid is the one of the meager ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 818we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 975As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 751There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 959Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 974As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 993As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1327in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 936class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 985template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1209There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 849the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 587The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1165In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 868A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1184You can overload the subscript ...
相关推荐
第一章 引言:光放大器的再探讨 班纳吉·库马尔·乔杜里 更多信息可在章节末尾获取 DOI:10.5772/intechopen.78671 暂定章节 引言 信息时代可以被视为快速、高带宽通信的时代,其中光纤通信系统发挥了关键作用。...
例如,如果仅考虑VO,则可以忽略属性Revisit_Freq 。 建议进行交叉验证,以免模型过度拟合。 根据决策树的结构复杂度(分类粒度),可以使用三种级别的决策树,从简单树到复杂树。 如果采用交叉验证,则相同的配置...
Create React App入门该项目是通过引导的。可用脚本在项目目录中,可以运行:npm start 在开发模式下运行应用程序。 打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。 您还将在控制台中看到任何棉绒错误...
附有论文供参考:[Revisit Long Short-Term Memory: An Optimization Perspective],NIPS 深度学习研讨会,2014。 #特征- 原始的长期短期记忆- 所有连接窥视Kong- CPU 或 GPU 加速- Mapreduce 并行化- 梯度检查- ...
Dynamic Data Structures: Patricia TriesThomas J. Repetti Maurice P. Herlihy Department of Computer ScienceBrown University {trepetti,mph}@cs.brown.eduAbstract The advent of multi-core microprocessors ...
在计算机视觉、计算机图形学和机器人领域,一个常见的问题是给定两个充分对齐的三维点云,计算它们之间的紧密对齐。... ...现代深度相机可以生成深度和颜色图像对,许多工业3D扫描仪也配备了同步色彩摄像头,并能将颜色...
总结起来,"Rethink, Revisit, Revise A Spiral Reinforced Self-Revised Network for Zero-Shot Learning" 提出了一种新颖的策略,通过螺旋式学习和自我修订,改进了ZSL中的语义关联学习。这种方法不仅提高了模型的...
- You want to revisit an interesting website that you came across before; However, you couldn't remember its address. Have you ever wondered what you in - You have spent a whole day on the web ...
网络爬虫Web-Crawler 是使用最少的组件构建的HTTP 获取器页面提取器政策 - PageFetch、重访访问过的 URL 消除器数据存储为了解析页面,使用了 Jsoup 库 ( ) 来解析 HTML。 这里给出的实现在以下方面与架构不同进程内...
C++ source codes OJ 序号 题目 描述 status 1 A+B Problem 基本输入输出 2 big int 超长整数相加 3 link 将两个线性表合并成为一个线性表 4 edit 字符串操作 5 二哥摘苹果 6 二哥种花生 7 二哥养细菌 8 西西弗斯式...
revisit-severson-et-al.ipynb :包含大部分分析和图形生成的Python笔记本。 image_annotated_heatmap.py :来自matplotlib的辅助函数(请参阅docstring获取源代码)。 此外,我们在nn_models目录中使用了三个...
【信达国际控股对信利国际00732.HK的研究报告】 信利国际,成立于1978年,是中国最大的手机显示屏供应商和第二大电容触摸屏供应商,客户群多元化,主要针对国内品牌,包括三星、小米、OPPO、Vivo等。...
在IT行业中,Git和GitHub是两个非常重要的工具,它们在版本控制和协同开发领域占据了核心地位。Git是一款分布式版本控制系统,而GitHub则是一个基于Git的在线托管平台,提供了丰富的社交编程功能。...
: :red_square: 完全中断该存储库包含由支持的的开源正常运行时间监控器和状态页面。 借助 ,您可以获得自己不受限制的免费正常运行时间监控器和状态页面,该页面完全由GitHub存储库提供支持。...
通过设置重访时间,可以指导搜索引擎多久之后再次访问网页,如:`<meta name="revisit-after" content="7 days">` 二、http-equiv属性 1. Expires(期限) 这个属性可以设置网页的过期时间,促使浏览器在指定...
The GeoEye-1 satellite is equipped with some of the most advanced technology ever used in a commercial remote sensing system....revisit any point on Earth once every three days or sooner
- **Revisit**:重新访问 - **Revise**:修订 - **Super-**:表示“在……之上”,例如: - **Supervise**:监督 - **Tele-**:表示“远距离”,例如: - **Television**:电视 #### 4. 词汇变化 - **Let**:...
**clvisit:reVisit 客户端通讯器详解** `clvisit:reVisit` 是一个专为实现客户端与服务器端高效通信的工具,其主要功能是处理客户沟通的重访需求。在现代软件开发中,尤其是在构建Web应用或移动应用时,实时通信是...
旨在通过将人类置于人工社交增强现实环境中来攻击人类ANTLR 对Visual Studio Code的ANTLR4语言支持API蓝图弃用-弃用-Identity Manager-Keyrock动作脚本 -revisit是围绕特定主题的最新Twitter消息(tweets)的实时...