static_cast运算符是标准c++中的强制类型转换运算符。原先的c语言中是存在类型转换的,如int型直接转换为float型,为什么需要引入新的类型转换?目的是为了克服旧式c语言中类型转换的缺陷。旧式的类型转换是很强的一种语法,带来的后果是引入很多不易察觉的错误。static_cast比旧式的类型转换更具体,更安全,类型转换的错误能在编译期间发现。例如,const类型转换为非const类型在编译期间被认为是一个语法错误。
#include<iostream>
using namespace std ;
class animal {
public:
void print(void) const{
cout<<"this is a animal"<<endl;}
};
class fish : public animal{
public:
void print(void) const{
cout <<"this is a fish"<<endl;
}
};
void test(animal *animalPtr)
{
fish * fishPtr;
fishPtr=static_cast<fish *>(animalPtr);
//可以用static_cast进行基类向派生类的转换
// fishPtr= animalPtr;
//无效的类型转换
fishPtr->print();
animalPtr=fishPtr;
animalPtr->print();
} ;
int main()
{
animal theanimal;
test(&theanimal);
cin.get();
}
//输出的结果是
this is a fish
this is a animal
分享到:
相关推荐
使用标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast。 const_cast,字面上理解就是去const属性。 static_cast,命名上理解是静态类型转换。如int转换成char。 dynamic_cast,...
dynamic_cast 运算符可以针对两种数据类型做强制转换:指针类型和引用类型。这两种类型的情况是不一样的。 使用 dynamic_cast 进行数据类型的转换的情况: * 对指针的强制转换。 * 对引用的强制转换。 dynamic_...
`static_cast`适用于执行标准类型间的转换,包括数值类型之间的转换、指针或引用的向上转型、使用单参数构造函数的转换以及使用转换运算符的转换。`static_cast`不会进行运行时检查,因此如果转换不适当,可能导致...
C++提供了四种标准的强制类型转换运算符,分别是`static_cast`、`dynamic_cast`、`reinterpret_cast`和`const_cast`。这些运算符在不同的情景下有不同的用途和限制,下面我们逐一详细探讨。 1. `static_cast` `...
本文详细介绍了C++中的四个用与强制类型转换的运算符:用来修改类型的const 或volatile 属性的const_cast,用来修改操作数类型的reinterpret_cast,static_cast,dynamic_cast
dynamic_cast 运算符 将操作数 expression 转换成类型为type-id 的对象。 语法 dynamic_cast < type> ( expression ) 备注 type-id 必须是一个指针或引用到以前已定义的类类型的引用或“指向 void 的指针”。如果 ...
static_cast; reinterpret_cast; 一、dynamic_cast 该运算符我在之前的文章中已经介绍过了 //www.jb51.net/article/123252.htm 总之,该运算符的语法如下: dynamic_cast < type> (expression) 如果转型失败则...
使用标准C++的类型转换符:static_cast、dynamic_cast、reinterpret_cast和const_cast。 1、static_cast 用法:static_cast (expression) 该运算符把expression转换为type-id类型,但没有运行时类型检查来...
const_cast,reinterpret_cast,static_cast,dynamic_cast等等。 1)static_cast(a) 将地址a转换成类型T,T和a必须是指针、引用、算术类型或枚举类型。 表达式static_cast(a), a的值转换为模板中...
在运算符重载中,`static_cast`常用于类型转换,尤其是在多态类型和指针之间。 五、注意事项 1. 避免在不明确的情况下进行类型转换,以免引入难以察觉的错误。 2. 重载运算符时,确保其行为符合用户的预期,保持...
8. `static_cast` 静态类型转换运算符 9. `const_cast` 常量类型转换运算符 10. `reinterpret_cast` reinterpret 类型转换运算符 11. `new` 创建动态内存对象运算符 12. `delete` 释放对象运算符 13. `delete[]` ...
显式类型转化(如`static_cast`,`dynamic_cast`,`reinterpret_cast`,`const_cast`)则由程序员手动指定,用于更精确地控制类型转换过程。 以下是一些关于类型转化的例子: ```cpp int i = 5; double d = i; // ...
在C++中,还有一种传统的强制类型转换方式,如`(type)expression`,虽然在某些情况下仍然可用,但现代C++推荐使用`static_cast`、`const_cast`、`dynamic_cast`和`reinterpret_cast`,因为它们提供了更明确的语义和...
在强制类型转换方面,C++引入了C风格的类型转换,如(int*),同时提供了更安全的静态类型转换(static_cast)、const_cast、dynamic_cast和reinterpret_cast。这些转换允许开发者明确指定如何进行类型之间的转换,降低...
在C++中,static_cast和reinterpret_cast是两种常用的类型转换运算符。static_cast用于类层次结构中类型之间的转换,如向上转型(派生类转基类)和向下转型(基类转派生类),以及非多态类型之间的转换。例如,可以...
C++提供了多种强制类型转换运算符,如`static_cast`、`dynamic_cast`、`reinterpret_cast`和`const_cast`。这些运算符用于在不同类型的对象间进行显式转换,以实现编译时或运行时的类型转换。其中,`static_cast`常...
42. static_cast:static_cast运算符用于将一个类型转换为另一个类型。 43. struct:结构体,用于定义结构体类型。 44. switch:交换,用于switch语句中。 45. template:模板,用于泛型编程。 46. this:当前对象,...
bytes[0] = static_cast((value >> 24) & 0xFF); bytes[1] = static_cast((value >> 16) & 0xFF); bytes[2] = static_cast((value >> 8) & 0xFF); bytes[3] = static_cast(value & 0xFF); } int main() { int ...
- C++引入了新的类型转换运算符,如 `static_cast<>`、`dynamic_cast<>`、`const_cast<>` 和 `reinterpret_cast<>`。这些运算符旨在提供更安全、更清晰的类型转换方式。由于它们是C++语言的关键部分,因此不允许被...