正确使用const_cast<> [英] Correct usage(s) of const_cast<>
本文介绍了正确使用const_cast<>的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
作为一个常见的规则,在C ++代码中使用 const_cast<>()
常常被认为是一种不好的做法,因为它揭示了(大部分时间)a设计中的缺陷。
虽然我完全同意这一点,但是我不知道什么是使用 const_cast< code>是 ok 和唯一解决方案。
你能给我一些例子
解决方案
const_cast
也用于删除 volatile
修饰符,这在这篇有争议的文章中已经付诸实践: http://www.drdobbs.com/184403766
As a common rule, it is very often considered a bad practice to use const_cast<>()
in C++ code as it reveals (most of the time) a flaw in the design.
While I totally agree with this, I however wonder what are the cases were using const_cast<>()
is ok and the only solution.
Could you guys please give me some examples you know/you encountered ?
Thank you very much.
解决方案
const_cast
is also used to remove volatile
modifiers, as put into practice in this (controversed) article:
http://www.drdobbs.com/184403766
这篇关于正确使用const_cast<>的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文