由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - C++ cast 小结
相关主题
c++ 语法一个C++ 的问题
c++ 弱问题:static const char* const 这两个const 分别是什么意思?why use static function here?
为什么cpp会有 const_cast ?C++ question
问题How to check the virtual function table size?
看道c++题: brianbench请问如何把初始化一个const 的vector (or array) in a class?
问题: C++ static_cast between int and floatC++默认的copy constructor的疑惑
const_cast问题What're the three types of memory allocated for C++ variables?
请教各路C++大神 为什么f(3) 输出是 'dd'请教c++ interface class问题
相关话题的讨论汇总
话题: cast话题: static话题: const话题: upcast
进入Programming版参与讨论
1 (共1页)
f******n
发帖数: 90
1
最近研究了一下cast, 成果如下,大家看看如何:
const_cast:
It's only used to add or remove qualifiers: const or volatile. Note: static
_cast can add const and can never remove const.
reinterpret_cast:
It never does any check, neither compile time nor runtime. But it requires
the two types have the same storage size in memory.
static_cast VS dynamic_cast:
In the inheritance hierarchy (all public inheritance), upcast will always
work, even without any type of explicit cast. static_cast will work for
downcast too wi
X****r
发帖数: 3557
2
你漏了一条:
只要T t(e);是well-formed就可以static_cast(e)

static
requires

【在 f******n 的大作中提到】
: 最近研究了一下cast, 成果如下,大家看看如何:
: const_cast:
: It's only used to add or remove qualifiers: const or volatile. Note: static
: _cast can add const and can never remove const.
: reinterpret_cast:
: It never does any check, neither compile time nor runtime. But it requires
: the two types have the same storage size in memory.
: static_cast VS dynamic_cast:
: In the inheritance hierarchy (all public inheritance), upcast will always
: work, even without any type of explicit cast. static_cast will work for

f******n
发帖数: 90
3
Thanks for the extra note!
I will add another one:
Both static_cast and dynamic_cast respect access controls. That means if
the inheritance is not public, compiler will report error when you try to
upcast.
k**m
发帖数: 222
4
总结得不错。
a*******s
发帖数: 324
5
for the upcast, do you mean derived --->base?
downcast, base-->derived?
Thanks,

【在 f******n 的大作中提到】
: Thanks for the extra note!
: I will add another one:
: Both static_cast and dynamic_cast respect access controls. That means if
: the inheritance is not public, compiler will report error when you try to
: upcast.

1 (共1页)
进入Programming版参与讨论
相关主题
请教c++ interface class问题看道c++题: brianbench
c++里面caveats太多了问题: C++ static_cast between int and float
有关objec access path的问题const_cast问题
问两个问题,C++请教各路C++大神 为什么f(3) 输出是 'dd'
c++ 语法一个C++ 的问题
c++ 弱问题:static const char* const 这两个const 分别是什么意思?why use static function here?
为什么cpp会有 const_cast ?C++ question
问题How to check the virtual function table size?
相关话题的讨论汇总
话题: cast话题: static话题: const话题: upcast