静态对象强制类型转换

  大家还记得我们前阵子制造出一个苹果公司么?没错,这节课我们还继续对这家公司入手进行讲解!Example.cpp:

#include<iostream>
#include<string>
 
class Company
{
    public:
        Company(std::string theName, std::string product);
        virtual void printInfo();//声明为虚函数
         
    protected:
        std::string name;
        std::string product;
};
 
class TechCompany : public Company
{
    public:
        TechCompany(std::string theName, std::string product);
        virtual void printInfo();
} ;
 
Company::Company(std::string theName, std::string product)
{
    name = theName;
    this->product = product;
}
 
void Company::printInfo()
{
    std::cout<<"这个公司的名字叫:"<<name<<"is producting"<< product	<<"\n";
}
 
TechCompany::TechCompany(std::string theName, std::string product):Company(theName, product)
{
}
 
void TechCompany::printInfo()
{
    std::cout<<name<<"公司大量生产了"<<product<<"这款产品!\n";
}
 
int main()
{
    Company *company = new TechCompany("APPLE", "Iphone");
    TechCompany *techCompany = (TechCompany *)company;//强制类型转换,若无强制转换会报错。 
    
	techCompany->printInfo(); 
    delete company;
    delete techCompany; //不需要本句,因为company,techCompany指向同一块地址,无需重复释放,会报错 
    company = NULL;
    techCompany = NULL; //本句需要 
     
    return 0;
}

要点总结:
  我们用传统的强制类型转换实现:把所需要的括针类型放在一对圆括号之间,然后写出将被强制转换的地址值。

Company *company = new Company("APPLE","Iphone"); 
TechCompany *tecCompany = company; 

  注意不能既删除company,又删除tecCompany。
  因为强制类型转换操作不会创建一个副本拷贝,它只是告诉编译器把有关变量解释为另一种类型组合形式,所以他们指向的是同一个地址。现在术语称之为“重婚”!只需要释放一个就行。

 

动态对象强制类型转换
  虽然刚刚那个栗子程序看起来很美!但它仍有一个问题没有解决:万一被强制转换的类型和目标类型结构完全不同,咋整?
  编译器很笨的,它仍然将按照我们的代码行事!这样子的程序是相当危险的,随时可能前绩以及被前绩。
  因为在类继承关系之间跳来转去(也就是对有关对象进行强制类型转换)在面向对象的程序里非常重要,所以C++程序员准备了几个新的强制类型转换操作符(高级)!注意:第二个最为常用。

  注:只要你喜欢,你仍可以在C++里继续用C的强制
  转换操作符(像刚才的栗子),但表中的操作符还能进行必要的类型检查,因而能够改善程序的可靠性。动态强制类型转换的语法与刚刚我们学到的有很大不同,它看起来更像是一个函数调用:

Company *company = new Company("APPLE", "Iphone");
TechCompany *tecCompany = dynamic_cast< TechCompany*>(company);

  先在两个尖括号之间写出想要的指针类型,然后是接被转换的值写在括号中。
  那我们一起来动手改改刚才的栗子吧,让它看上去更美丽一些:

#include<iostream>
#include<string>
 
class Company
{
    public:
        Company(std::string theName, std::string product);
        virtual void printInfo();//声明为虚函数
         
    protected:
        std::string name;
        std::string product;
};
 
class TechCompany : public Company
{
    public:
        TechCompany(std::string theName, std::string product);
        virtual void printInfo();
} ;
 
Company::Company(std::string theName, std::string product)
{
    name = theName;
    this->product = product;
}
 
void Company::printInfo()
{
    std::cout<<"这个公司的名字叫:"<<name<<"is producting"<< product	<<"\n";
}
 
TechCompany::TechCompany(std::string theName, std::string product):Company(theName, product)
{
}
 
void TechCompany::printInfo()
{
    std::cout<<name<<"公司大量生产了"<<product<<"这款产品!\n";
}
 
int main()
{
    Company *company = new TechCompany("APPLE", "Iphone");
    TechCompany *techCompany = dynamic_cast<TechCompany *>(company);//强制类型转换,若无强制转换会报错。 
    
	techCompany->printInfo(); 
    delete company;
 
    company = NULL;
    techCompany = NULL; //本句需要 
     
    return 0;
}

  注意第二个转换操作符:如果value的类型不是一个MyClass类(或MyClass的子类)的指针,这个操作符将返回NULL。一定运行看看哈!

#include<iostream>
#include<string>
 
class Company
{
    public:
        Company(std::string theName, std::string product);
        virtual void printInfo();//声明为虚函数
         
    protected:
        std::string name;
        std::string product;
};
 
class TechCompany : public Company
{
    public:
        TechCompany(std::string theName, std::string product);
        virtual void printInfo();
} ;
 
Company::Company(std::string theName, std::string product)
{
    name = theName;
    this->product = product;
}
 
void Company::printInfo()
{
    std::cout<<"这个公司的名字叫:"<<name<<"is producting"<< product	<<"\n";
}
 
TechCompany::TechCompany(std::string theName, std::string product):Company(theName, product)
{
}
 
void TechCompany::printInfo()
{
    std::cout<<name<<"公司大量生产了"<<product<<"这款产品!\n";
}
 
int main()
{
    Company *company = new Company("APPLE", "Iphone");
    TechCompany *techCompany = dynamic_cast<TechCompany *>(company);//强制类型转换,若无强制转换会报错。 
    
    if(techCompany != NULL)
    {
    	std::cout<<"成功!\n";
    }
    else
	{
		std::cout<<"悲催!\n";
	} 
	
	techCompany->printInfo(); //本句错误,因为techCompany现在指向的是NULL指针
    delete company;
 
    company = NULL;
    techCompany = NULL;
     
    return 0;
}

 

  

 

转载于:https://www.cnblogs.com/tianqizhi/p/10488768.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值