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 | | 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.
|
|