Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > casting in VC++ 6

Reply
Thread Tools

casting in VC++ 6

 
 
hswerdfe
Guest
Posts: n/a
 
      08-10-2005
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:oStuff(CBase* other);
and I want it to call CDerived1:oStuff(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


 
Reply With Quote
 
 
 
 
Victor Bazarov
Guest
Posts: n/a
 
      08-10-2005
hswerdfe wrote:
> 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:oStuff(CBase* other);
> and I want it to call CDerived1:oStuff(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:oStuff
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:oStuff
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
 
Reply With Quote
 
 
 
 
hswerdfe
Guest
Posts: n/a
 
      08-10-2005
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



"Victor Bazarov" <(E-Mail Removed)> wrote in message
news:HkrKe.28270$(E-Mail Removed)01.us.to .verio.net...
> hswerdfe wrote:
>> 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:oStuff(CBase* other);
>> and I want it to call CDerived1:oStuff(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:oStuff
> 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:oStuff
> 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



 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Up casting and down casting Sosuke C++ 2 12-20-2009 03:24 PM
Problem with depracated casting method (down casting) Wally Barnes C++ 3 11-20-2008 05:33 AM
type casting vs. type converting Toby VHDL 3 09-07-2005 01:42 PM
Another question about inheritance (up-casting and down-casting) kevin Java 11 01-08-2005 07:11 PM
'STD_LOGIC_VECTOR ' to 'unsigned' type casting Ben Nguyen VHDL 6 09-20-2003 05:09 PM



Advertisments