C++关于类型转换引入了四种方式:
使用场景:基本数据类型之间的转换使用,例如float转int,int转char等,在有类型指针和void*之间转换使用,子类对象指针转换成父类对象指针也可以使用static_cast。
非多态类型转换一般都使用static_cast,而且最好把所有的隐式类型转换都是用static_cast进行显示替换,不能使用static_cast在有类型指针之间进行类型转换。
主要有如下几种用法:
(1)用于类层次结构中基类和派生类之间指针或引用的转换
进行上行转换(把派生类的指针或引用转换成基类表示)是安全的
进行下行转换(把基类的指针或引用转换为派生类表示),由于没有动态类型检查,所以是不安全的
(2)用于基本数据类型之间的转换,如把int转换成char。这种转换的安全也要开发人员来保证
(3)把空指针转换成目标类型的空指针
(4)把任何类型的表达式转换为void类型
注意:static_cast不能转换掉expression的const、volitale或者__unaligned属性。
static_cast:可以实现C++中内置基本数据类型之间的相互转换。
如果涉及到类的话,static_cast只能在有相互联系的类型中进行相互转换,不一定包含虚函数。
int a = 1;
int b = 2;
double result = static_cast<double>(a) / static_cast<double>(b);
使用场景:用于常量指针或引用与非常量指针或引用之间的转换,只有const_cast才可以对常量进行操作,一般都是用它来去除常量性,去除常量性是危险操作,还是要谨慎操作。
const限定符通常被用来限定变量,用于表示该变量的值不能被修改。而const_cast则正是用于强制去掉这种不能被修改的常数特性,但需要特别注意的是const_cast不是用于去除变量的常量性,而是去除指向常数对象的指针或引用的常量性,其去除常量性的对象必须为指针或引用。
用法:const_cast
该运算符用来修改类型的const或volatile属性。除了const 或volatile修饰之外, type_id和expression的类型是一样的。
常量指针被转化成非常量指针,并且仍然指向原来的对象;
常量引用被转换成非常量引用,并且仍然指向原来的对象;常量对象被转换成非常量对象。
const_cast关键字强制去掉指针的常量性:
#include
const int * Search(const int * a, int n, int val);
int main(){
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int val = 5;
int *p;
p = const_cast<int *>(Search(a, 10, val));
if (p == NULL)
std::cout << "not found" << std::endl;
else
std::cout << "found" << std::endl;
}
const int * Search(const int *a, int n, int val)
{
int i;
for(i = 0; i < n; i++){
if(a[i] == val)
return &a[i];
}
return NULL;
}
const_cast关键字强制去掉引用的常量性:
#include
const int & Search(const int * a, int n, int val);
int main(){
int a[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int val = 5;
int &p = const_cast<int &>(Search(a, 10, val));
if (p == NULL)
std::cout << "not found" << std::endl;
else
std::cout << "found" << std::endl;
}
const int & Search(const int *a, int n, int val)
{
int i;
for(i = 0; i < n; i++){
if(a[i] == val)
return a[i];
}
return NULL;
}
使用场景:用于将父类的指针或引用转换为子类的指针或引用,此场景下父类必须要有虚函数,因为dynamic_cast是运行时检查,检查需要运行时信息RTTI,而RTTI存储在虚函数表中
用法:dynamic_cast
(1)其他三种都是编译时完成的,dynamic_cast是运行时处理的,运行时要进行类型检查。
(2)不能用于内置的基本数据类型的强制转换。
(3)dynamic_cast转换如果成功的话返回的是指向类的指针或引用,转换失败的话则会返回NULL。
(4)使用dynamic_cast进行转换的,基类中一定要有虚函数,否则编译不通过。
B中需要检测有虚函数的原因:类中存在虚函数,就说明它有想要让基类指针或引用指向派生类对象的情况,此时转换才有意义。
这是由于运行时类型检查需要运行时类型信息,而这个信息存储在类的虚函数表(关于虚函数表的概念,详细可见
只有定义了虚函数的类才有虚函数表。
(5)在类的转换时,在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的。在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全。
向上转换,即为子类指针指向父类指针(一般不会出问题);向下转换,即将父类指针转化子类指针。
向下转换的成功与否还与将要转换的类型有关,即要转换的指针指向的对象的实际类型与转换以后的对象类型一定要相同,否则转换失败。
在C++中,编译期的类型转换有可能会在运行时出现错误,特别是涉及到类对象的指针或引用操作时,更容易产生错误。Dynamic_cast操作符则可以在运行期对可能产生问题的类型转换进行测试。
#include
using namespace std;
class base{
public:
void m(){
cout << "m" << endl;
}
};
class derive: public base{
public:
void f(){
cout << "f" << endl;
}
};
int main(){
derive * p;
// 使用static_cast不会报错
p = static_cast<derive *>(new base);
// 使用dynamic_cast会报错,指向基类时,没有f()函数
// 因为base类中并不存在虚函数,因此p = dynamic_cast(new base);这一句会编译错误。
p = dynamic_cast<derive *>(new base);
p->m();
p->f();
return 0;
}
#include
using namespace std;
class base{
public:
virtual void m(){
cout << "m" << endl;
}
};
class derive: public base{
public:
void m(){
cout << "derive m" << endl;
}
};
class derive_2: public base{
void n(){
cout << "n" << endl;
}
};
int main(){
base * a1 = new derive;
base * a2 = new base;
derive * b;
derive_2 * c;
b = dynamic_cast<derive*>(a1); //结果为not null,向下转换成功,a1之前指向的就是derive类型的对象,所以可以转换成derive类型的指针。
if(b == NULL)
cout << "NULL" << endl;
else
cout << "not NULL" << endl;
b = dynamic_cast<derive*>(a2); //结果为null,向下转换失败
if(b == NULL)
cout << "NULL" << endl;
else
cout << "not NULL" << endl;
c = dynamic_cast<derive_2*>(a1); //结果为null,向下转换失败
if(c == NULL)
cout << "NULL" << endl;
else
cout << "not NULL" << endl;
return 0;
}
使用场景:类似C语言中的强制类型转换,什么都可以转,万不得已不要使用,一般前三种转换方式不能解决问题了使用这种强制类型转换方式
reinterpret_cast主要有三种强制转换用途:改变指针或引用的类型、将指针或引用转换为一个足够长度的整形、将整型转换为指针或引用类型。
用法:reinterpret_cast
type-id必须是一个指针、引用、算术类型、函数指针或者成员指针。
它可以把一个指针转换成一个整数,也可以把一个整数转换成一个指针(先把一个指针转换成一个整数,在把该整数转换成原类型的指针,还可以得到原先的指针值)。
在使用reinterpret_cast强制转换过程仅仅只是比特位的拷贝,因此在使用过程中需要特别谨慎!
int *a = new int;
double *b = reinterpret_cast<double *>(a);