- 浏览: 391963 次
- 性别:
- 来自: 上海
文章分类
- 全部博客 (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 15298首先你需要下载的是 Microsoft Windows S ... -
rpath - runtime search path
2013-04-03 11:36 977RPath is a very interesting to ... -
C++ - autogenerated copy constructor and assignment operator gotchas
2013-01-24 13:32 749It has been changed that the s ... -
c++ - rethrow a exception gotchas
2012-12-23 10:57 930As in my prevoius example in j ... -
c++ -typeid operator
2012-10-15 22:30 1040typeid is the one of the meager ... -
c++ - virtual inheritance example 1
2012-10-14 15:25 791we have discussed the virtual i ... -
c++ - virtual inheritance
2012-10-12 08:58 930As we have discussed in the pos ... -
c++ type of inheritance
2012-09-28 08:58 733There are 3 types of inheritanc ... -
c++ - vritually virtual new
2012-09-27 23:59 940Let's see what if we want to cl ... -
c++ - virtual destructor
2012-09-27 22:01 933As we all know that virtual des ... -
c++ - vritual function and default arguments
2012-09-27 08:56 962As we all know that we virtual ... -
c++ - template specialization and partial specialization
2012-09-26 22:38 1298in this post, we are going to e ... -
c++ - member template in class template
2012-09-26 08:19 912class member template can be us ... -
c++ template class and the pattern to use its friends
2012-09-25 23:47 966template class may defined thei ... -
c++ - Friend declaration in class Template
2012-09-25 08:47 1190There are three kinds of friend ... -
c++ - class template default parameters
2012-09-25 08:18 819the template has parameter, it ... -
c++ - operator new and delete and an example of linked list stores by new/delete
2012-09-24 07:53 560The operator new and delete ope ... -
c++ - delete(void *, size_t) or delete(void *)
2012-09-24 07:18 1141In my previous dicuss, we have ... -
c++ - placement operator new() and the operator delete()
2012-09-23 15:22 843A class member operator new() c ... -
c++ - overloaded subscript operator - []
2012-09-23 08:50 1140You can overload the subscript ...
相关推荐
Introductory Chapter_ A Revisit to Optical Amplifiers
例如,如果仅考虑VO,则可以忽略属性Revisit_Freq 。 建议进行交叉验证,以免模型过度拟合。 根据决策树的结构复杂度(分类粒度),可以使用三种级别的决策树,从简单树到复杂树。 如果采用交叉验证,则相同的配置...
Rethink, Revisit, Revise A Spiral Reinforced Self-Revised Network for Zero-Shot Learning_重新思考、重新审视、修订一个螺旋强化的自我修订网络,用于零射击学习.pdf
Create React App入门该项目是通过引导的。可用脚本在项目目录中,可以运行:npm start 在开发模式下运行应用程序。 打开在浏览器中查看它。 如果您进行编辑,则页面将重新加载。 您还将在控制台中看到任何棉绒错误...
This book is for developers or students who want to revisit basic data structures and algorithm design techniques. Although no mathematical background is required, basic knowledge of complexity ...
附有论文供参考:[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 ...
(Colored ICP算法)ICCV2017_Colored Point Cloud Registration Revisited1
- 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目录中使用了三个...
港股公司研究
回顾git命令和github
: :red_square: 完全中断该存储库包含由支持的的开源正常运行时间监控器和状态页面。 借助 ,您可以获得自己不受限制的免费正常运行时间监控器和状态页面,该页面完全由GitHub存储库提供支持。...
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
旨在通过将人类置于人工社交增强现实环境中来攻击人类ANTLR 对Visual Studio Code的ANTLR4语言支持API蓝图弃用-弃用-Identity Manager-Keyrock动作脚本 -revisit是围绕特定主题的最新Twitter消息(tweets)的实时...
LSTM-MATLAB is Long Short-term Memory (LSTM) in MATLAB, which is meant to be succinct, ... It is accompanied with a paper for reference Revisit Long Short-Term Memory An Optimization Perspective, NIPS
LSTM-MATLAB is Long Short-term Memory (LSTM) in MATLAB, which is meant to be succinct, ... It is accompanied with a paper for reference Revisit Long Short-Term Memory An Optimization Perspective, NIPS
LSTM-MATLAB is Long Short-term Memory (LSTM) in MATLAB, which is meant to be succinct,... It is accompanied with a paper for reference Revisit Long Short-Term Memory An Optimization Perspective, NIPS.zip