casting in VC++ 6

H

hswerdfe

I have several Classes that derive from a common Base Class
Each of these Classes Has a Differnt Version of a Function with the Same
Short name, but different Long Name.

See Bellow:

// Base Class
class CBase
{
void DoStuff(CBase* other);
}

//Derived Class 1
class CDerived1: public CBase
{
void DoStuff(CDerived1* other);
}

// Derived Class 2
class CDerived1: public CBase
{
void DoStuff(CDerived1* other);
}


What I want to do is Create A new object of the Same Type that is being
passed into my function
then call the apropriate function
my code currently looks like this:

void OtherFunction(CBase* MyDerived)
{
CBase* myNewBase = (CBase*)
MyDerived->GetRuntimeClass()->CreateObject();
myNewBase->DoStuff(MyDerived);
}

but this always calls CBase::DoStuff(CBase* other);
and I want it to call CDerived1::DoStuff(CDerived1* other);
or one of the other derived functions
the only way I can think of doing this is to use a lot of

if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived1)))
//cast then call
if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived2)))
//cast then call
if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived3)))
//cast then call


is there a better way to call the Derived Classes Member function?


thanks,

how
 
V

Victor Bazarov

hswerdfe said:
I have several Classes that derive from a common Base Class
Each of these Classes Has a Differnt Version of a Function with the Same
Short name, but different Long Name.

See Bellow:

// Base Class
class CBase
{
void DoStuff(CBase* other);
}

//Derived Class 1
class CDerived1: public CBase
{
void DoStuff(CDerived1* other);
}

// Derived Class 2
class CDerived1: public CBase
{
void DoStuff(CDerived1* other);
}


What I want to do is Create A new object of the Same Type that is being
passed into my function
then call the apropriate function
my code currently looks like this:

void OtherFunction(CBase* MyDerived)
{
CBase* myNewBase = (CBase*)
MyDerived->GetRuntimeClass()->CreateObject();
myNewBase->DoStuff(MyDerived);
}

but this always calls CBase::DoStuff(CBase* other);
and I want it to call CDerived1::DoStuff(CDerived1* other);
or one of the other derived functions
the only way I can think of doing this is to use a lot of

if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived1)))
//cast then call
if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived2)))
//cast then call
if (myNewBase->IsKindOf(RUNTIME_CLASS(CDerived3)))
//cast then call


is there a better way to call the Derived Classes Member function?

The only way I know is to let the derived classes handle it all. It's
called polymorphism and is created with _virtual_ functions:

class Base {
public
virtual void DoStuff(Base*);
};

class Derived1 : Base {
/* non-virtual */ void DoStuff(Derived1 *pd); // whatever
void DoStuff(Base* pb) { // this one overrides the Base::DoStuff
Derived1* pd1 = dynamic_cast<Derived1*>(pb);
if (pd1)
this->DoStuff(pd1);
}
};

class Derived2 : Base {
/* non-virtual */ void DoStuff(Derived2 *pd); // whatever
void DoStuff(Base* pb) { // this one overrides the Base::DoStuff
Derived2* pd2 = dynamic_cast<Derived2*>(pb);
if (pd2)
this->DoStuff(pd2);
}
};

This is a very short reply, may be considered incomplete, but look it over
and ask more questions if you get any. Along with it, read about virtual
functions in your favourite C++ book.

Oh, and we don't talk VC++ specific stuff here. If you need help with any
of Visual C++ specific things, try 'microsoft.public.vc.language'.

V
 
H

hswerdfe

thanks, this is the conclusion I came to as well.
all the 'DoStuff' functions are already virtual.
problem is
CBase, CDerived1, CDerived2....
have all been created by my boss and he mostly considers them untouchable.

sorry about the off topic Post
I just reposted to 'microsoft.public.vc.language'

p.s. if you have any other sollutions let me know on either board
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top