C++ 基本多态性

c++ basic polymorphism

本文关键字:多态性 本多 C++      更新时间:2023-10-16
class Product
{
...
}
class Perishable : public : Product
{
 public:
 int getday();
}

int main()
{
   Product *temp;
   //due to some coding
   //temp could point to either Perishable object or Product object that is determine           //during runtime
   cout<< ((Perishable*)temp)->getday() ;// is there other way to achieve this typecasting seems dangerous

这段代码的问题在于,如果 temp 指向 Product 对象,temp->getday(( 将是无效的,我不知道如何防止这种情况发生。如果由于某些情况,我只允许在易腐烂中使用getday((,而不允许在产品中使用getday((,我如何检查temp是否指向易腐烂的对象或产品对象?

一些帮助将不胜感激/

}

"这段代码的问题在于,如果 temp 指向 Product 对象,temp->getday(( 将是无效的,我不知道如何防止这种情况发生。

本着问题的精神,如果你绝对不想在你的产品类中声明/实现 getday((,如其他答案中所述,你可以使用动态强制转换来确定变量的运行时类型,然后只在你有一个易腐实例时才调用 getday((:

  Product* pPerishable = new Perishable;
  Product* pProduct = new Product;
  Perishable * pActualPerishable;
  pActualPerishable= dynamic_cast<Perishable *>(pPerishable );
  //pActualPerishable will not be null because it is of type Perishable at run time
  pActualPerishable = dynamic_cast<Perishable*>(pProduct );
  //pActualPerishable will be null because you are trying to cast a runtime base type to a derived type.
因此,尝试将变量

动态转换为易腐烂的变量,如果成功,则知道可以调用getday((。 请注意,这不再是多态的,但在运行时确定类型有其用途,尤其是在您无法控制正在处理的对象的接口的情况下。

我认为你需要的是:

class Product 
{ 
public:  
    virtual int getday() = 0;  
}  
class Perishable : public : Product 
{  
public:  
    virtual int getday();  
}   

通过此更改,您现在可以执行以下操作:

cout  << temp->getday();

这与多态性有什么关系?我假设 getDay(( 也在产品中定义?如果是这样,那么这就是继承和多态性的全部目的。您应该能够致电

temp->getday((; 完全不用担心演员阵容。只要 temp 确实是一个产品或其衍生物之一,并且 getDate(( 在 Product 中被定义为虚拟的,就不需要任何强制转换。

例:

class Product
{
public:
   virtual int getday();
};
class Perishable: public Product
{
public:
  virtual int getday();   
};
int main()
{
   Product *temp; //= you should be getting the new from some factory method somewhere.
   //Polymorphism will handle making sure that the right function is called here.
   cout<< temp->getday();
}

有几种方法,例如:

class Product
{
    int getday() { return ... ; } // -1 for example or any invalid value
}
class Perishable : public : Product
{
public:
    int getday();
}

class Product
{
    virtual int getday() = 0;
}
class Perishable : public : Product
{
public:
    virtual int getday(); // You must implement this somewhere
}

多态性需要一个虚拟基方法,然后在子类中重写该方法,如下所示:

class Product
{
   virtual int getday(); // You could also make this pure abstract
}
class Perishable : public Product
{
 public:
 int getday();
}
class NonPerishable : public Product
{
 public:
 int getday();
}

int main()
{
   Product *temp;
   temp = new Perishable();       
   cout << temp->getday(); // Perishable::getday()
   temp = new NonPerishable();       
   cout << temp->getday(); // NonPerishable::getday()
}

如果getDay是类Product virtual函数,则不需要强制转换。你可以简单地写这个:

cout<< temp->getday();

如果 temp 指向类型 Product 的对象,则将调用Product:getDay。如果temp指向类型为 Perishable 的对象,则Perishable::getDay如果在 Perishable 中被覆盖,则将调用该对象,否则将调用Product::getDay

这就是运行时多态性的工作原理。

class Product 
{ 
... 
} 
class Perishable : public : Product 
{ 
 public: 
 int getday(); 
} 

int main() 
{ 
   Product *temp; 
   //due to some coding 
   //temp could point to either Perishable object or Product object that is determine           //during runtime 
   Product *pObj = dynamic_cast<Perishable *>(temp);
   if(!temp)
   {
       temp->getday();   //temp is containing Perishable Object.
   }
   else {
       //it is Product Obj;
   }
}

这个概念被称为RTTI。要查找对象 temp 类型的名称,您也可以使用 typeid(temp(::name((,但这只会返回对象 temp 指向的类型。它不会为您进行类型转换,因此请使用dynamic_cast。