您当前的位置: 首页 > 学无止境 > 心得笔记 网站首页心得笔记
第37讲-高级强制类型转换
发布时间:2021-05-21 21:43:55编辑:雪饮阅读()
静态对象强制类型转换
先看下面的例子
#include <iostream>
#include <string>
using namespace std;
//公司類
class Company
{
public:
Company(string theName, string product)
{
name = theName;
this -> product = product;
}
virtual void printInfo()
{
cout << "这个公司的名字叫: " << name <<
"正在生产" << product << "\n";
}
protected:
string name;
string product;
};
//技術公司類
class TechCompany:public Company
{
public:
TechCompany(string theName, string product):Company(theName, product)
{
}
virtual void printInfo()
{
cout << name << "公司大量生产了" << product << "这款产品!\n";
}
};
int main()
{
Company *company = new TechCompany("APPLE", "IPHONE");
//sizeof 返回字節大小
cout<<"company size:"<<sizeof(company)<<"\n";
TechCompany *tecCompany = company;
tecCompany -> printInfo();
//刪除company指針指向的内存
delete company;
//將company指針自身幹掉
company = NULL;
tecCompany = NULL;
return 0;
}
編譯會報錯
按理來説這都是相同的類型,父類接收一個子類實例生成父類的實例,然後子類創建實例並以剛才這個父類實例來初始化。
從流程上看起來應該是沒有問題的。
但是這裏就是報錯了,其實這涉及到了强制類型轉換。
有點像是Java中的向上轉型和向下轉型一樣。
传统的强制类型转换
我们用传统的强制类型转换实现:把所需要的指针类型放在一对圆括号之间,然后写出将被强制转换的地址值。
#include <iostream>
#include <string>
using namespace std;
//公司類
class Company
{
public:
Company(string theName, string product)
{
name = theName;
this -> product = product;
}
virtual void printInfo()
{
cout << "这个公司的名字叫: " << name <<
"正在生产" << product << "\n";
}
protected:
string name;
string product;
};
//技術公司類
class TechCompany:public Company
{
public:
TechCompany(string theName, string product):Company(theName, product)
{
}
virtual void printInfo()
{
cout << name << "公司大量生产了" << product << "这款产品!\n";
}
};
int main()
{
Company *company = new TechCompany("APPLE", "IPHONE");
//sizeof 返回字節大小
cout<<"company size:"<<sizeof(company)<<"\n";
TechCompany *tecCompany = (TechCompany*)company;
tecCompany -> printInfo();
//刪除company指針指向的内存
delete company;
//將company指針自身幹掉
company = NULL;
tecCompany = NULL;
return 0;
}
編譯並運行結果如:
company size:8
APPLE公司大量生产了IPHONE这款产品!
Process returned 0 (0x0) execution time : 0.013 s
Press any key to continue.
注意不能既删除company,又删除tecCompany。因为强制类型转换操作不会创建一个副本拷贝,它只是告诉编译器把有关变量解释为另一种类型组合形式,所以他们指向的是同一个地址。现在术语称之为“重婚”
动态对象强制类型转换
虽然刚刚那个例子程序看起来很美!但它仍有一个问题没有解决:万一被强制转换的类型和目标类型结构完全不同,咋整?
编译器很笨的,它仍然将按照我们的代码行事!这样子的程序是相当危险的,随时可能崩溃。
因为在类继承关系之间跳来跳去(也就是对有关对象进行强制类型转换)在面向对象的程序里非常重要,所以C++程序员准备了几个新的强制类型转换操作符(高级)!
注:只要你喜欢,你仍可在C++里继续使用C的强制转换操作符(像刚才的例子)。
现在两个尖括号之间写出想要的指针类型,然后是将被转换的值写在括号中。
则完整的实例:
#include <iostream>
#include <string>
using namespace std;
//公司類
class Company
{
public:
Company(string theName, string product)
{
name = theName;
this -> product = product;
}
virtual void printInfo()
{
cout << "这个公司的名字叫: " << name <<
"正在生产" << product << "\n";
}
protected:
string name;
string product;
};
//技術公司類
class TechCompany:public Company
{
public:
TechCompany(string theName, string product):Company(theName, product)
{
}
virtual void printInfo()
{
cout << name << "公司大量生产了" << product << "这款产品!\n";
}
};
int main()
{
Company *company = new TechCompany("APPLE", "IPHONE");
//sizeof 返回字節大小
cout<<"company size:"<<sizeof(company)<<"\n";
/*
动态对象强制类型转换
原型:dynamic_cast<MyClass*>(value)
如果value的类型不是一个MyClass类(或MyClass的子类)的指针,这个操作符将返回NULL
*/
TechCompany *tecCompany = dynamic_cast<TechCompany *>(company);
if (tecCompany != NULL)
{
cout << "成功!" << endl;
}
else
{
cout << "悲催!\n";
}
tecCompany -> printInfo();
//刪除company指針指向的内存
delete company;
//將company指針自身幹掉
company = NULL;
tecCompany = NULL;
return 0;
}
编译并运行结果如:
company size:8
成功!
APPLE公司大量生产了IPHONE这款产品!
Process returned 0 (0x0) execution time : 0.056 s
Press any key to continue.
那么直接将父类转为子类自然不行了,因为对象最初实例化的时候就不是子类
#include <iostream>
#include <string>
using namespace std;
//公司類
class Company
{
public:
Company(string theName, string product)
{
name = theName;
this -> product = product;
}
virtual void printInfo()
{
cout << "这个公司的名字叫: " << name <<
"正在生产" << product << "\n";
}
protected:
string name;
string product;
};
//技術公司類
class TechCompany:public Company
{
public:
TechCompany(string theName, string product):Company(theName, product)
{
}
virtual void printInfo()
{
cout << name << "公司大量生产了" << product << "这款产品!\n";
}
};
int main()
{
Company *company = new Company("APPLE", "IPHONE");
//sizeof 返回字節大小
cout<<"company size:"<<sizeof(company)<<"\n";
TechCompany *tecCompany = dynamic_cast<TechCompany *>(company);
if (tecCompany != NULL)
{
cout << "成功!" << endl;
}
else
{
cout << "悲催!\n";
}
tecCompany -> printInfo();
//刪除company指針指向的内存
delete company;
//將company指針自身幹掉
company = NULL;
tecCompany = NULL;
return 0;
}
编译并运行结果如:
company size:8
悲催!
Process returned -1073741819 (0xC0000005) execution time : 0.415 s
Press any key to continue.
关键字词:强制类型转换,高级强制类型转换
上一篇:第36讲-副本构造器
下一篇:第39讲-命名空间和模块化编程
相关文章
-
无相关信息