关于本文主题还有下面这篇博客可以参考:https://blog.csdn.net/komtao520/article/details/79025562
tip——string转char*的库函数方法:
string.c_str();
类型转换
静态转换(static_cast)
使用方式:
static_cast<目标类型>(原始对象)
用法:
- 用于父类和子类之间指针或引用的转换。
- 用于基本数据类型之间的转换,如把int转换成char,把char转换成int。这种转换的安全性也要开发人员来保证。
- 没有父子关系的自定义类型不可以转换
补充:
- 进行上行转换(把派生类的指针或引用转换成基类表示)是安全的;
- 进行下行转换(把基类指针或引用转换成派生类表示)时,由于没有动态类型检查,所以是不安全的。
基本数据类型转换的例子:
-
void test01(){
-
char a =
'a';
-
double d =
static_cast<
double>(a);
-
cout <<
"d = " << d <<
endl;
-
}
继承关系指针互相转换
-
class Base{};
-
class Child :
public Base{};
-
class Other{};
-
void test02(){
-
Base * base =
NULL;
-
Child * child =
NULL;
-
-
//把base转为 Child*类型 向下 不安全
-
Child * child2 =
static_cast<Child*>(base);
-
-
//把child 转为 Base* 向上 安全
-
Base * base2 =
static_cast<Base*>(child);
-
-
//转other类型 转换无效
-
//Other * other = static_cast<Other*>(base);
-
}
继承关系引用相互转换
-
class Animal{};
-
class Dog :
public Animal{};
-
class Other{};
-
-
void test03(){
-
-
Animal ani_ref;
-
Dog dog_ref;
-
//继承关系指针转换
-
Animal& animal01 = ani_ref;
-
Dog& dog01 = dog_ref;
-
//子类指针转成父类指针,安全
-
Animal& animal02 =
static_cast<Animal&>(dog01);
-
//父类指针转成子类指针,不安全
-
Dog& dog02 =
static_cast<Dog&>(animal01);
-
}
动态转换(dynamic_cast)——十分严格
使用方式:
dynamic_cast<目标类型>(原始对象)
用法:
- 基础类型不可以转换
- dynamic_cast主要用于类层次间的上行转换和下行转换;
- 在类层次间进行上行转换时,dynamic_cast和static_cast的效果是一样的;
- 在进行下行转换时,dynamic_cast具有类型检查的功能,比static_cast更安全;
- 发生多态,则允许发生向上转换和向下转换。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20190116161605972.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3FxXzI5OTk2Mjg1,size_16,color_FFFFFF,t_70)
dynamic_cast 如果发生了多态,那么可以让基类转为派生类 ,向下转换
原因:
创建空间大小的时候,是按照子类的大小进行创建的。
使用样例:
-
class Animal {
-
public:
-
virtual void ShowName() =
0;
-
};
-
class Dog :
public Animal{
-
virtual void ShowName(){
-
cout <<
"I am a dog!" <<
endl;
-
}
-
};
-
class Other {
-
public:
-
void PrintSomething(){
-
cout <<
"我是其他类!" <<
endl;
-
}
-
};
-
-
//普通类型转换
-
void test01(){
-
-
//不支持基础数据类型
-
int a =
10;
-
//double a = dynamic_cast<double>(a);
-
}
-
-
//继承关系指针
-
void test02(){
-
-
Animal* animal01 =
NULL;
-
Dog* dog01 =
new Dog;
-
-
//子类指针转换成父类指针 可以
-
Animal* animal02 =
dynamic_cast<Animal*>(dog01);
-
animal02->ShowName();
-
//父类指针转换成子类指针 不可以
-
//Dog* dog02 = dynamic_cast<Dog*>(animal01);
-
}
-
-
//继承关系引用
-
void test03(){
-
-
Dog dog_ref;
-
Dog& dog01 = dog_ref;
-
-
//子类引用转换成父类引用 可以
-
Animal& animal02 =
dynamic_cast<Animal&>(dog01);
-
animal02.ShowName();
-
}
-
-
//无继承关系指针转换
-
void test04(){
-
-
Animal* animal01 =
NULL;
-
Other* other =
NULL;
-
-
//不可以
-
//Animal* animal02 = dynamic_cast<Animal*>(other);
-
}
常量转换(const_cast)
该运算符用来修改类型的const属性。
使用方式:
- 加上const: const int * newP2 = const_cast<const int *>(p2);
- 去掉const: int * newp = const_cast<int *>(p);
用法:
- 常量引用被转换成非常量引用,并且仍然指向原来的对象;
- 常量指针被转化成非常量指针,并且仍然指向原来的对象;
- 不能直接对非指针和非引用的变量使用const_cast操作符去直接移除它的const.
-
//常量指针转换成非常量指针
-
void test01(){
-
-
const
int* p =
NULL;
-
int* np =
const_cast<
int*>(p);
-
-
int* pp =
NULL;
-
const
int* npp =
const_cast<
const
int*>(pp);
-
-
const
int a =
10;
//不能对非指针或非引用进行转换
-
//int b = const_cast<int>(a); }
-
-
//常量引用转换成非常量引用
-
void test02(){
-
-
int num =
10;
-
int & refNum = num;
-
-
const
int& refNum2 =
const_cast<
const
int&>(refNum);
-
-
}
重新解释转换(reinterpret_cast)——最不安全,不推荐
主要用于将一种数据类型从一种类型转换为另一种类型。它可以将一个指针转换成一个整数,也可以将一个整数转换成一个指针.
-
void test06(){
-
int a =
10;
-
int * p =
reinterpret_cast<
int *>(a);
-
-
Base * base =
NULL;
-
Other * other =
reinterpret_cast<Other*>(base);
-
-
//最不安全 ,不推荐
-
}