多态与下铸 [英] Polymorphism vs DownCasting

查看:130
本文介绍了多态与下铸的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一个基类Employee和一个派生类Manager,如下所示:

Let's say I have a Base class Employee and an derived class Manager, like below:

class Employee
{
public:
    string Name;  
};

class Manager : public Employee
{
public:
    string Designation;
};

同时实现如下功能:

Employee* SomeFunction(bool SomeCondition)
{
    Employee *Emp = NULL;
    if (SomeCondition) 
    {
       //Code goes here : Both Implementation 1 and 2 work fine!          
    }              
    return Emp;
}

SomeCondition为true时,我想返回类型为Manager的非空对象.在这种情况下,下面的两个代码段似乎都符合要求:

When SomeCondition is true, I want to return a non-null object of type Manager. In such a scenario, both below pieces of code seem to fit the bill:

实施1:

Manager *Mng = new Manager;
Mng->Name = "Adam";
Mng->Designation = "BOSS";
Emp = Mng;

实施2:

Emp = new Manager;
Manager *Mng = (Manager*)Emp;
Mng->Name = "Adam";
Mng->Designation = "BOSS";

既然两者都能正常工作,我想知道两者中哪一个效率更高?

Since both work just fine, I would like to know which one among the two is the more efficient one?

哪个使用多态性概念?

实现2中的类型转换是否是向下转换?这是好习惯吗?

Is the type casting in Implementation 2 a down-cast? Is it good practice?

推荐答案

虽然我看到您的问题背后有一些原因,但我认为您 需要改进您的示例

While I see some reasons behind your questions, I think you need to improve the your example

  • 您是在说您需要返回一个非空对象 类型为经理",而您定义为"SomeFunction(bool SomeCondition)" 返回员工".

  • you are saying that you need to return a non-null object of type "Manager", while your define "SomeFunction(bool SomeCondition)" to return "Employee".

如果您确实要返回"Employee"对象,为什么要打扰 初始化名称"时将无法访问它 之后.例如:

if you are indeed going to return "Employee" object why bothering initializing "Designation" while you will not be able to access it later. For example:

cout << SomeFunction(true)->Designation(); // error !  

因此,我不确定说您的示例可以正常工作意味着什么, 因为上下文不清楚.

So, I'm not sure what do mean by saying your examples work fine, since the context is not clear.

**比较实施1和2/关于动态转换

** Comparing Implementation 1 and 2 / About dynamic casting

虽然两个示例都可以改进,但我认为实现1略有改进 更好的.在这两种情况下,您都进行动态投射.但是,在实施中 在图1中,您在实现2中进行了"Emp = Mng;"的隐式上载 您可以在"Manager Mng =(Manager )Emp;"中进行向下转换.

While both examples can improve, I think Implementation 1 is slightly better. In both cases you do dynamic casting. However, in Implementation 1, you do an implicit upcasting in "Emp = Mng;", while in Implementation 2 you do downcasting in "Manager Mng = (Manager)Emp;".

通常,您应避免进行强制转换(尤其是向下转换,因为它是 与上流相比,并不是一直那么安全),如果您必须 您应该使用C ++样式转换(例如dynamic_cast).见 例子中 https://www.tutorialcup.com/cplusplus/upcasting-downcasting.htm

In general you should avoid casting (especially the downcasting since it's not that safe all the time compared with upcasting), and if you have to you should use C++ style casting (e.g. dynamic_cast). See the example in https://www.tutorialcup.com/cplusplus/upcasting-downcasting.htm

更好的解决方案是使用虚函数以避免 并腾出空间在管理器"旁边添加更多对象类型. 例如,您的标题可能如下所示:

A better solution is to use virtual functions in order to avoid casting and make room to add more objects types beside "Manager". For example, your header may look like:

class Employee
{
  public:
    virtual void setDesignation(const string & d) = 0;
    virtual string getDesignation() = 0; 
};

class Manager : public Employee
{
  public:
    virtual void setDesignation (const string & d) {Designation=d;}
    virtual string getDesignation() {return Designation;}

  private:
    string Designation;
};

和您的函数可能看起来像:

and your function may look like:

Employee* SomeFunction(bool SomeCondition)
{
   Employee *Emp = NULL;
   if (SomeCondition) 
   {
     Emp = new Manager;;
     Emp->setDesignation("BOSS");             
   }              
   return Emp;
}

然后,如果您以后想要访问该名称,则可以

then if you want to access the Designation later, you can do

cout << SomeFunction(true)->getDesignation();

**关于多态性

在两个示例中都没有使用任何多态性.这是因为你不 使用任何特定于类型的函数,因此您的运行时行为不会 根据雇员"对象的不同而有所不同(您仅使用一种对象类型 还是经理"!).请参阅中的示例 http://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm

you don't use any Polymorphism in both examples. This is becuase you don't use any function that is type-specific, and so your runtime behaviour doesn't vary depending on the "Employee" object (you are merely using one object type "Manager" anyways !). See the example in http://www.tutorialspoint.com/cplusplus/cpp_polymorphism.htm

这篇关于多态与下铸的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆