C++中用dynamic_cast实现downcast之一见
来源:互联网 发布:厦门智业软件 编辑:程序博客网 时间:2024/04/29 10:58
Almost alway it will be on the right track if we use "dynamic_cast " for up-casting, with either a pointer or a reference, to a type of sub-class from that of base-class, and this would be guaranteed to be safe in most cases. Nevertheless possibly, if you ever deploy it by yourself, it will not act as you expect if you use this casting operator for down-casting instead. For me, not so that lucky it is!
Now let 's show you the scenario as this: Originally I dynamically create an object of the sub-class and assign the returned pointer to a pointer to the base-class, then actually I just intend to cast this pointer to base-class back to pointer to the sub-class, which seems absolutely reasonable and nothing unpredictable would be expected, but thing only to run out of its normal way!
Passing through the compilation is entirely ok, but when I trigger the program, an run-time error bursts out, which is concerning such and such a memory allocation on the heap. Then I try the same case for downcasting a reference instead of pointer, the result stands the same, in the end ,therefore, I am buffled!
After nearly a week of being bewildered and puzzled, edificed by a post issued from a fellow CSDNer, I recall a critical try -- the compilation option that controls the support for RTTI. Woo! I cry, doesn 't the dynamic-cast depend on the sustenance of RTTI by the run-time system?
Definitely that 's the key I 've been dredging toiling to search! Immediately after I open the option in the compilation configuration, everything goes extremely right!
Now let 's show you the scenario as this: Originally I dynamically create an object of the sub-class and assign the returned pointer to a pointer to the base-class, then actually I just intend to cast this pointer to base-class back to pointer to the sub-class, which seems absolutely reasonable and nothing unpredictable would be expected, but thing only to run out of its normal way!
Passing through the compilation is entirely ok, but when I trigger the program, an run-time error bursts out, which is concerning such and such a memory allocation on the heap. Then I try the same case for downcasting a reference instead of pointer, the result stands the same, in the end ,therefore, I am buffled!
After nearly a week of being bewildered and puzzled, edificed by a post issued from a fellow CSDNer, I recall a critical try -- the compilation option that controls the support for RTTI. Woo! I cry, doesn 't the dynamic-cast depend on the sustenance of RTTI by the run-time system?
Definitely that 's the key I 've been dredging toiling to search! Immediately after I open the option in the compilation configuration, everything goes extremely right!
- C++中用dynamic_cast实现downcast之一见
- 【C/C++学习】之五、dynamic_cast
- C++dynamic_cast
- C++ 使用dynamic_cast进行downcast时常会出现的问题 [RTTI]
- 【C/C++学习】之二、static_cast和dynamic_cast、reinterpret_cast
- C语言中用结构实现位段
- C语言中用bsearch()实现查找操作
- C语言中用GNU gettext实现本地化
- C语言中用GNU gettext实现本地化
- C 语言中用bsearch()实现查找操作
- C 语言中用bsearch()实现查找操作
- c++_static_cast and dynamic_cast
- c++: dynamic_cast详解
- 实现自己的dynamic_cast
- C++ dynamic_cast实现原理
- C++之dynamic_cast
- C++晋升之dynamic_cast
- C++晋升之dynamic_cast
- 无法进入系统
- JSP数据库连接池的必要性
- http://cometdaily.com/
- 搭建完美JSP开发环境
- 海外共享软件营销模式实例分析
- C++中用dynamic_cast实现downcast之一见
- LUA使用笔记
- JFreeChart开发股票K线图
- Java中ImageIcon的使用
- 从VFS inode到LFS inode的寻址过程
- eclipse 不自动编译java文件的问题.
- 世界上并无汉语编程——正如世界上并无英语编程
- 针对mysql中mysqldump命令使用的详解
- 如何才能学好一门技术!