一、静态对象强制类型转换
大家还记得我们前阵子制造出一个苹果公司么?没错,这节课我们还继续对这家公司入手进行讲解!
为了演示方便,小甲鱼小小的做了一些修改,尽管这样的修改有些问题,但是,作为今日内容的讲解还是甚好滴
#include <iostream>
#include <string>
using namespace std;
class Company
{
public:
Company(string theName, string product);
virtual void printInfo();
protected:
string name;
string product;
};
class TechCompany:public Company
{
public:
TechCompany(string theName, string product);
virtual void printInfo();
};
Company::Company(string theName, string product)
{
name = theName;
this -> product = product;
}
void Company::printInfo()
{
cout << "这个公司的名字叫: " << name <<
"正在生产" << product << "\n";
}
TechCompany::TechCompany(string theName, string product):Company(theName, product)
{
}
void TechCompany::printInfo()
{
cout << name << "公司大量生产了" << product << "这款产品!\n";
}
int main()
{
Company *company = new TechCompany("APPLE", "IPHONE");
TechCompany *tecCompany = company;
tecCompany -> printInfo();
delete company;
company = NULL;
tecCompany = NULL;
return 0;
}
运行结果
test.cpp:48:28: error: invalid conversion from ‘Company*’ to ‘TechCompany*’ [-fpermissive]
48 | TechCompany *tecCompany = company;
| ^~~~~~~
| |
| Company*
首先company接收的是new开辟的一块32位的地址空间,之后再将其给tecCompany,但是为啥不能这样呢?
强制类型转换:
TechCompany *tecCompany = (TechCompany *)company;
二、 传统的强制类型转换
我们用传统的强制类型转换实现:把所需要的指针类型放在一对圆括号之间,然后写出将被强制转换的地址值。
#include <iostream>
#include <string>
using namespace std;
class Company
{
public:
Company(string theName, string product);
virtual void printInfo();
protected:
string name;
string product;
};
class TechCompany:public Company
{
public:
TechCompany(string theName, string product);
virtual void printInfo();
};
Company::Company(string theName, string product)
{
name = theName;
this -> product = product;
}
void Company::printInfo()
{
cout << "这个公司的名字叫: " << name <<
"正在生产" << product << "\n";
}
TechCompany::TechCompany(string theName, string product):Company(theName, product)
{
}
void TechCompany::printInfo()
{
cout << name << "公司大量生产了" << product << "这款产品!\n";
}
int main()
{
Company *company = new TechCompany("APPLE", "IPHONE");
TechCompany *tecCompany = (TechCompany *)company;/*c语言传统的类型转化,产生一个问题*/
tecCompany -> printInfo();
delete company;
delete tecCompany;/*重复释放*/
company = NULL;
tecCompany = NULL;
return 0;
}
运行结果
APPLE公司大量生产了IPHONE这款产品!
free(): double free detected in tcache 2
已放弃 (核心已转储
注意不能既删除company,又删除tecCompany。因为强制类型转换操作不会创建一个副本拷贝,它只是告诉编译器把有关变量解释为另一种类型组合形式,所以他们指向的是同一个地址。现在术语称之为“重婚”
三、动态对象强制类型转换
虽然刚刚那个例子程序看起来很美!但它仍有一个问题没有解决:万一被强制转换的类型和目标类型结构完全不同,咋整?
编译器很笨的,它仍然将按照我们的代码行事!这样子的程序是相当危险的,随时可能崩溃以及被崩溃。
因为在类继承关系之间跳来跳去(也就是对有关对象进行强制类型转换)在面向对象的程序里非常重要,所以C++程序员准备了几个新的强制类型转换操作符(高级)!
注:只要你喜欢,你仍可在C++里继续使用C的强制转换操作符(像刚才的例子),但表中的操作符还能进行必要的类型检查,因而能够改善程序的可靠性。
Company * company = new Company("APPLE", "IPHONE");
TechCompany * tecCompany = dynamic_cast<TechCompany *>(company)
现在两个尖括号之间写出想要的指针类型,然后是将被转换的值写在括号中。
#include <iostream>
#include <string>
using namespace std;
class Company
{
public:
Company(string theName, string product);
virtual void printInfo();
protected:
string name;
string product;
};
class TechCompany:public Company
{
public:
TechCompany(string theName, string product);
virtual void printInfo();
};
Company::Company(string theName, string product)
{
name = theName;
this -> product = product;
}
void Company::printInfo()
{
cout << "这个公司的名字叫: " << name <<
"正在生产" << product << "\n";
}
TechCompany::TechCompany(string theName, string product):Company(theName, product)
{
}
void TechCompany::printInfo()
{
cout << name << "公司大量生产了" << product << "这款产品!\n";
}
int main()
{
Company *company = new Company("APPLE", "IPHONE");
TechCompany *tecCompany = dynamic_cast<TechCompany *>(company);
if (tecCompany != NULL)
{
cout << "成功!" << endl;
}
else
{
cout << "悲催!\n";
}
tecCompany -> printInfo();
delete company;
company = NULL;
tecCompany = NULL;
return 0;
}
运行结果:
悲催!
段错误 (核心已转储)
这是因为不同对象导致的
#include <iostream>
#include <string>
using namespace std;
class Company
{
public:
Company(string theName, string product);
virtual void printInfo();
protected:
string name;
string product;
};
class TechCompany:public Company
{
public:
TechCompany(string theName, string product);
virtual void printInfo();
};
Company::Company(string theName, string product)
{
name = theName;
this -> product = product;
}
void Company::printInfo()
{
cout << "这个公司的名字叫: " << name <<
"正在生产" << product << "\n";
}
TechCompany::TechCompany(string theName, string product):Company(theName, product)
{
}
void TechCompany::printInfo()
{
cout << name << "公司大量生产了" << product << "这款产品!\n";
}
int main()
{
Company *company = new Company("APPLE", "IPHONE");
TechCompany *tecCompany = dynamic_cast<TechCompany *>(company);
if (tecCompany != NULL)
{
cout << "成功!" << endl;
}
else
{
cout << "悲催!\n";
}
//tecCompany -> printInfo();
delete company;
company = NULL;
tecCompany = NULL;
return 0;
}
运行结果:
悲催!
最后程序的输出为:悲催!说明两个不同类型的结构不能用于强制类型转换。
修改为相同类型
#include <iostream>
#include <string>
using namespace std;
class Company
{
public:
Company(string theName, string product);
virtual void printInfo();
protected:
string name;
string product;
};
class TechCompany:public Company
{
public:
TechCompany(string theName, string product);
virtual void printInfo();
};
Company::Company(string theName, string product)
{
name = theName;
this -> product = product;
}
void Company::printInfo()
{
cout << "这个公司的名字叫: " << name <<
"正在生产" << product << "\n";
}
TechCompany::TechCompany(string theName, string product):Company(theName, product)
{
}
void TechCompany::printInfo()
{
cout << name << "公司大量生产了" << product << "这款产品!\n";
}
int main()
{
Company *company = new Company("APPLE", "IPHONE");
Company *tecCompany = dynamic_cast<Company *>(company);
if (tecCompany != NULL)
{
cout << "成功!" << endl;
}
else
{
cout << "悲催!\n";
}
tecCompany -> printInfo();
delete company;
company = NULL;
tecCompany = NULL;
return 0;
}
运行结果:
成功!
这个公司的名字叫: APPLE正在生产IPHONE