avatar
f*n
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
avatar
X*r
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

avatar
f*n
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.
avatar
k*m
4
总结得不错。
avatar
a*s
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.

相关阅读
logo
联系我们隐私协议©2024 redian.news
Redian新闻
Redian.news刊载任何文章,不代表同意其说法或描述,仅为提供更多信息,也不构成任何建议。文章信息的合法性及真实性由其作者负责,与Redian.news及其运营公司无关。欢迎投稿,如发现稿件侵权,或作者不愿在本网发表文章,请版权拥有者通知本网处理。