Click here to Skip to main content
15,887,446 members
Please Sign up or sign in to vote.
3.00/5 (2 votes)
See more:
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
Posted
Updated 1-Jun-11 21:46pm
v2
Comments
Sergey Alexandrovich Kryukov 2-Jun-11 2:31am    
It looks like interview/test questions. Bad ones. Who will be interested to answer?
My 1. I don't feel the interest in technology or practical need.
--SA
pjhelp 2-Jun-11 2:39am    
Hi SA,
Please try to share your good knowledge if you have and let other to share their knowledge.I thing its a good way to enrich our knowledge.So please.
Thanks
Sergey Alexandrovich Kryukov 2-Jun-11 2:50am    
Absolutely. I do welcome anyone to do it and do myself, a lot. Let me refrain from answering these questions though -- they irritate me with their incorrectness. This is not the right way to get knowledge.
--SA
Jaadooo 2-Jun-11 5:18am    
ok man can you assist me ....
i am vc+++ guy with 8+ years exp. , i am looking for a change , but i found that market is not inclied towards VC++ ....now tell me what shall i do to value my vc+ knoledge

Suppose you have a polymorphic object
C++
class PolyObj
{
public:
    virtual ~PolyObj() {}
    virtual bool compare(const PolyObj& s) const =0;
    ....

    bool operator==(const PolyObj& s) const
    { return compare(s); }
};


And suppose you have many different implementations ObjA, ObjB, ObjC all derived by PolyObj.

One way to implement compare in ObjA can be:
C++
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
    }
};



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[^])
 
Share this answer
 
v2
RTTY usage is probably never compulsory and, in my opinion, should be avoided.
:-)
 
Share this answer
 
v2
Comments
Emilio Garavaglia 2-Jun-11 16:59pm    
RTTI exist for valid reasons. It can be avoided if misused, but can be required exactly as virtual functions (see below)
CPallini 3-Jun-11 1:58am    
Even in your scenario is not compulsory.
Emilio Garavaglia 3-Jun-11 5:26am    
Of course: the only "compulsory" things are a value comparer, a conditional selector, and an iteration mechanism. Everything else is "Touring equivalent" and can be avoided. But we can substantially approximate "compulsory" with "extremely convenient" (and the evaluation is subjective). That's the only way I found to have this question to make sense.
CPallini 3-Jun-11 5:41am    
Well that's for the Touring completeness. However C++ supports OOP and hence the 'pillars' of OOP are 'required' too. Now RTTI is not such and, in my opinion is not generally 'extremely convenient'.
Stefan_Lang 7-Jul-11 8:20am    
I'd agree to your comment for those cases where you have full control over the complete source code and all library interfaces. But that is a situation that unfortunately never occurs!

The example in Solution 1 is a perfect example: Of course, the dynamic_cast wouldn't be required with a little redesign of the classes, but since they are in another library, you may not be able to change them!
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();
	}
}

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
 
Share this answer
 
Comments
pjhelp 2-Jun-11 14:16pm    
Thanks U very much Anna.
Having seen a very similarly phrased question from you earlier, I can only presume you have a lot of homework questions and very little time left to complete it!
 
Share this answer
 
Comments
pjhelp 2-Jun-11 14:16pm    
Dear Dave,
We are expecting your guide lines [if you want to share like Resmi Anna (above)] on this concept.Thanks
Case 1:
--My project support multipurpose languages
--User selected a language ( ex. Spanish )
--based on the language object assigned to my variable ....i will link corresponding com component to my application ( these com components contains languages translation against the string id )

Case 2 :
--My application supports diffident mobile handsets
--Once it will found the type of mobile connected to it
--connection device will call corresponding dll to configure / monitor the mobile
 
Share this answer
 
Comments
Stefan_Lang 7-Jul-11 8:26am    
Both cases do not require RTTI. You can use a simple inheritance scheme, or just simple values. Using a class to identify an instance is just wrong design.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900