"enki" <en*****@yahoo.com> wrote in message
news:11**********************@f14g2000cwb.googlegr oups.com...
I have been doing some reading on dynamic bindind and polymorphism. I
know the basics from what I have seen in books. The way virtual
methods were explained was that they were used when methods the same
methods were inhereted into 2 objects and they are parents of new
objects.
I would like some situations where polymorphism is useful.
My next step will to try to understand handle classes.
Here's one: I got a project with several different object-types that needs
to be updated (run) on a regular basis, so I use
baseclasses such as this:
// baseclass for type-id
class CObjectID
{
public:
virtual CObjectID* GetClass(DWORD iId){return (iId == OID_OBJECTID) ? this :
NULL;}
};
// 'baseclass' for data-exchange
class CBaseContext : public CObjectID
{
public:
virtual bool Validate() = 0;
virtual CObjectID* GetClass(DWORD iId){return (iId == OID_BASECONTEXT) ?
this : CObjectID::GetClass(iId);}
};
// 'baseclass' for objects
class CTask : public CObjectID
{
private:
CTask
*m_pcParent;
CTaskList
m_cChildList;
public:
virtual void Run(CBaseContext* pcContext); // calls run on all
childtasks
virtual CObjectID* GetClass(DWORD iId){return (iId == OID_TASK) ? this :
CObjectID::GetClass(iId);}
};
Now I can build a tree of pointers to objects inherited from CTask, call Run
on the root and have all the objects updated (objects calls CTask::Run() in
their own Run() function to run it's own children).
When working mostly with pointers to baseclasses, a type-id system is
sometimes useful to enable checking the actual type of an object, or if an
object is inherited from a given class (esp. useful for for asserts() and
validation), so CTask inherits from CObjectID which has a virtual GetClass()
that gives this functionality.
PKH