隐式转换:编译器根据需要自动转换变量类型。
1、一些基本类型的转换
double d = 82.0;
int i = d;
2、类的隐式转换,以下几种情况,类B能隐式转换成类A:
(1)B公有继承A,然后用子类去初始化基类
class B: class A{}
A a;
B b;
a =b;
(2)B中有类型转换函数
class B{
operator A();
};
A a;
B b;
a = b;
class A{
A(const B&);
};
A a;
B b;
a = b;
int *ip;
char* pc = (char*)ip;
C++有四大强制类型转换符:reinterpret_cast, const_cast, static_cast, dynamic_cast.使用形式为:cast-name<type>(expression)
1、reinterpret_cast:最普通的强制类型转换符,与用圆括号将类型括起来实现一样
int* ip;
char* pc = reinterpret_cast<char*>(ip);
2、const_cast:去掉表达式的const性质
const char* pc_str;
char* pc = const_cast<char*>(pc_str); //新的表达式pc则消除了const性质
class base{};
class child:public base{};
base* b;
child * c;
c = static_cast<child*>(b); //下行转换,正确
char* p = str;
int* p1 = static_cast<int*>(p);
//错误,指针不能有隐式转换,所以用static_cast也
//是错误的。
4、dynamic_cast:主要用于类层次间的上行转换和下行转换,也就是用于判断基类指针和子类指针的转换是否正确,如果错误则返回空指针。上行转换指用派生类去初始化基类,这是没问题的;下行转换,如果基类指针指向的是派生类,则转换成功,如果基类指针指向的是基类,则不能转换,返回空指针。dynamic_cast也可以用来转换兄弟类,如base1,base2相互转换。
dynamic_cast使用有几个前提:(1)type必须是类的指针、类的引用,或者void*(2)必须要有虚函数表,也就是说如果要进行类的类型转换,则必须是定义了虚函数的类。
class B{
public:
B():b(1){}
virtual void foo(){};
int b;
}
class D:public B{
public:
D():d(2){}
int d;
}
void fun(B* pb){
D* pd1 = dynamic_cast<D*>(pb);
}
B* pb = new D;
fun(pb);
则是正确的,因为基类的指针指向派生类,转换成功。而如果
B*pb = new B;
fun(pb);
则是错误的,因为基类的指针指向基类,不能用基类去初始化派生类,所以错误,此时pd1将返回空指针,将会发生异常,从而能够更早的发现错误。