473,386 Members | 1,823 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

casting in VC++ 6

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
Aug 10 '05 #1
2 1975
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::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
Aug 10 '05 #2
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" <v.********@comAcast.net> wrote in message
news:Hk******************@newsread1.mlpsca01.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::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

Aug 10 '05 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: dis | last post by:
I've been going through my code and clearing away some of the compiler warnings that i'm generating and i've come across areas where i cast pointers to integer values. The Visual Studio compiler...
4
by: 0to60 | last post by:
I'm coding in MC++ and I'm using the System::Collections data structures to store my own objects. When I get something out of a hashmap, should I be using dynamic_cast or old C-style casting? In...
6
by: Philipp Schumann | last post by:
Hi, I have a need for "dynamic type casting": in other words, in a "MyConvert" method I get passed an Object "value" and a Type "type" and the method should attempt to convert value into type. ...
4
by: DKode | last post by:
I started using Enums to make my code more readable. Here is my ENUM: public enum EntryType : int { RegularHours = 1, Lunch = 2, Vacation = 3, Sick = 4, Personal = 5 }
0
by: BobTheHacker | last post by:
You may have read about the problem I am having concering an "invalid cast" when coming from a C# dll into a VC++ exe. Anyway, I exposed the class in the C# code and derived an object from it and I...
44
by: Agoston Bejo | last post by:
What happens exactly when I do the following: struct A { int i; string j; A() {} }; void f(A& a) { cout << a.i << endl;
11
by: James Juno | last post by:
Hello, I want to do something like this: class A { ... }; class B : public A
11
by: Vinod | last post by:
Hi, I am working in the project where VC6 code is ported to VC8 (VC++ .Net 2005) I got a problem when I cast a double value to unsigned int. Problem is I couldn’t get the proper value after...
2
by: suresh | last post by:
Hi I saw this code in the net for converting case. string s = "Amma"; transform(s.begin(),s.end(),s.begin(),(int(*)(int))toupper); Q: what is the meaning of (int(*)(int))toupper ? The...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.