C++ 基本多态性
c++ basic polymorphism
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。
- 多态性和功能结合
- 找不到成员对象:没有名为get_event()的成员,也处理多态性和向量
- 使用取消引用的指针的多态性会产生意外的结果.为什么?
- C++boost序列化多态性问题
- 如何查找哪个类对象位于数组的特定索引上(多态性)
- 如何在多线程中正确使用unique_ptr进行多态性?
- 具有智能指针的多态性
- 在 C++ 中在堆栈上创建实例时如何保持多态性?
- 继承/多态性 - 我是否被迫使用"protected"变量?
- C++ 多态性在代码::块 17.12 中不起作用
- C++ 泛型和多态性:这种模式可行吗?
- 为什么我们实际上需要运行时多态性?
- 如何在这个简单的最小示例中实现多态性?
- 如何使用静态多态性在 int 和指针类型之间进行转换?
- 无法初始化已知大小的矢量指针,该大小不会因多态性而更改
- 运行时多态性和dynamic_cast需要澄清
- 如何调用指针类型的方法(禁用多态性)?
- 从基类调用函数的多态性
- 运行时多态性 - 箭头运算符访问了错误的成员?
- 为什么我在虚幻引擎中的多态性和接口方面遇到问题?