在OOP中必须使用RTTI的地方? [英] where it is compulsory to use RTTI in OOPs ?
问题描述
我看了很多文章,我得到了它的定义和实现方法.但是我对此没有任何具体想法.我的意思是说-
.
-为什么我们需要[实际用法]?
-可以替代吗?
-何时必须使用此功能?
.
.
等..
请详细指导我(实际用法\主要优点).谢谢
Hi,
I had gone through number of articles,I got it''s definition and how to implement. But i unable to get any concrete idea on this.I mean to say -
.
- Why we need [practical usage] this?
- Is there any substitution to this?
- When it is compulsory to use this?
.
.
etc..
Kindly please guide me in detail(Practical usage \ main advantages).Thanks
推荐答案
假设您有一个多态对象
Suppose you have a polymorphic object
class PolyObj
{
public:
virtual ~PolyObj() {}
virtual bool compare(const PolyObj& s) const =0;
....
bool operator==(const PolyObj& s) const
{ return compare(s); }
};
并假设您有许多不同的实现ObjA,ObjB,ObjC,它们都是由PolyObj派生的.
在ObjA中实现compare
的一种方法可以是:
And suppose you have many different implementations ObjA, ObjB, ObjC all derived by PolyObj.
One way to implement compare
in ObjA can be:
class ObjA: public PolyObj
{
A a;
B b;
public:
virtual bool compare(const PolyObj& s) const
{
const ObjA* pA = dynamic_cast<const ObjA*>(&s); // this requires RTTI
if(!pA) return false; //if not of the same runtime-type, cannot be equal.
return a == pA->a && b == pA->b; //now we can compare ObjA members
}
};
为ObjB和ObjC提供类似的实现,您将获得两个操作数的多态运算符==,只是三个函数而不是九个函数.
基本上,我们进行了双重调度":第一次是通过V表,第二次是通过RTTI.
当然,我们可以做不同的事情(例如,调用第二个对象的虚函数,然后调用第一个对象的虚函数),但是使用的代码要复杂得多.
(请参见多方法 [
Providing similar implementation for ObjB and ObjC, you get a polymorphic oprator== for both its operands, just with three functions instead of nine.
Basically we did a "dual dispatch": the first through the V-table, and the second through RTTI.
Of course, we can do it differently (like calling a virtual function of the second object that calls a virtual function of the first), but with much more complex code.
(see Multimethod[^])
RTTY
使用可能永远不是强制性的,我认为应该避免.
:-)
RTTY
usage is probably never compulsory and, in my opinion, should be avoided.
:-)
这在运行时不知道对象类型的情况下很有用.
例如看下面的代码
我将向您解释一个简单的用法.
This is useful in a situation where you don’t know the type of your object at run time.
For example look at below code
I shall explain you a simple usage of that.
// Code snippet1
class IBase
{
public:
virtual void Test()
{
// Do something
}
};
class Dervi1 : public IBase
{
public:
void Test()
{
// Do something
}
void Dervi1Function()
{
// Do something
}
};
class Dervi2 : public IBase
{
public:
void Test()
{
// Do something
}
void Dervi2Function()
{
// Do something
}
};
void Operation()
{
if( some condition )
{
MyFunction( new Dervi1 );
}
if( some condition )
{
MyFunction( new Dervi2 );
}
}
// Code snippet2
void MyFunction( IBase* pBase )
{
Dervi2* der2 = dynamic_cast<Dervi2*>( pBase );
Dervi1* der1 = dynamic_cast<Dervi1*>( pBase );
if( der2 )
{
der2->Dervi2Function();
}
else if( der1 )
{
der2->Dervi1Function();
}
}
现在,假设代码snippet1驻留在一个dll中,代码snippet2驻留在另一个dll中,并且您必须实现MyFunction(),并且设计人员会提供该原型.我可能会首先寻求我们需要RTTI支持的解决方案.当您使用具有某种复杂设计的应用程序时,这很有用
now suppose the code snippet1 resides in one dll and code snippet2 in another dll and you have to implement MyFunction() and you are provided with that protype by a designer. i may go for this solution first for which we need RTTI support.This is useful when you work with application having some kind of complicated design
这篇关于在OOP中必须使用RTTI的地方?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!