经常见到,但从来没弄明白过:
static_cast
const_cast
reinterpret_cast
最好解释详细一点,我太弱了。
static_cast
const_cast
reinterpret_cast
最好解释详细一点,我太弱了。
解决方案 »
- 如何改变slider背景颜色??
- 【讨论】一个困扰我好久的问题,本人比较笨
- 不同程序读写文件,如何避免冲突????????????
- 哪个老大能提供《VC++实效编程百例》 ~急
- 如何用VC++6.0的SDK写一个简单的画图板
- Dialog的stytle一设成 child(或popup)就会创建时报错退出,这是怎么回事?怎么办?
- 请教高手,如何在VIEW中做出类似于WINDOWS自带的图象处理程序中的可移动可缩放的文本编辑框?
- 关于在Win32 Application中使用CDialog做对话框程序的问题,100分,明天下午结
- 跪求~一本介绍关于线程的书
- 我编一个.c的文件,但在编译时会出现这样的错误,请问高手怎样解决?
- 怎么作Word插件
- 如何制作help文档?
The expression static_cast < type-id > ( expression ) converts expression to the type of type-id based solely on the types present in the expression. No run-time type check is made to ensure the safety of the conversion.Syntaxstatic_cast < type-id > ( expression )The static_cast operator can be used for operations such as converting a pointer to a base class to a pointer to a derived class. Such conversions are not always safe. For example:class B { ... };class D : public B { ... };void f(B* pb, D* pd)
{
D* pd2 = static_cast<D*>(pb); // not safe, pb may
// point to just B B* pb2 = static_cast<B*>(pd); // safe conversion
...
}In contrast to dynamic_cast, no run-time check is made on the static_cast conversion of pb. The object pointed to by pb may not be an object of type D, in which case the use of *pd2 could be disastrous. For instance, calling a function that is a member of the D class, but not the B class, could result in an access violation.The dynamic_cast and static_cast operators move a pointer throughout a class hierarchy. However, static_cast relies exclusively on the information provided in the cast statement and can therefore be unsafe. For example:class B { ... };
class D : public B { ... };void f(B* pb)
{
D* pd1 = dynamic_cast<D*>(pb);
D* pd2 = static_cast<D*>(pb);
}If pb really points to an object of type D, then pd1 and pd2 will get the same value. They will also get the same value if pb == 0. If pb points to an object of type B and not to the complete D class, then dynamic_cast will know enough to return zero. However, static_cast relies on the programmer’s assertion that pb points to an object of type D and simply returns a pointer to that supposed D object.Consequently, static_cast can do the inverse of implicit conversions, in which case the results are undefined. It is left to the programmer to ensure that the results of a static_cast conversion are safe.This behavior also applies to types other than class types. For instance, static_cast can be used to convert from an int to a char. However, the resulting char may not have enough bits to hold the entire int value. Again, it is left to the programmer to ensure that the results of a static_cast conversion are safe.The static_cast operator can also be used to perform any implicit conversion, including standard conversions and user-defined conversions. For example:typedef unsigned char BYTEvoid f()
{
char ch;
int i = 65;
float f = 2.5;
double dbl; ch = static_cast<char>(i); // int to char
dbl = static_cast<double>(f); // float to double
...
i = static_cast<BYTE>(ch);
...
}The static_cast operator can explicitly convert an integral value to an enumeration type. If the value of the integral type does not fall within the range of enumeration values, the resulting enumeration value is undefined.The static_cast operator converts a null pointer value to the null pointer value of the destination type.Any expression can be explicitly converted to type void by the static_cast operator. The destination void type can optionally include the const, volatile, or __unaligned attribute.The static_cast operator cannot cast away the const, volatile, or __unaligned attributes. See const_cast Operator for information on removing these attributes.
我也在MSDN上找到了。