473,738 Members | 10,643 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(CDerive d1* other);
}

// Derived Class 2
class CDerived1: public CBase
{
void DoStuff(CDerive d1* 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(C Base* MyDerived)
{
CBase* myNewBase = (CBase*)
MyDerived->GetRuntimeClas s()->CreateObject() ;
myNewBase->DoStuff(MyDeri ved);
}

but this always calls CBase::DoStuff( CBase* other);
and I want it to call CDerived1::DoSt uff(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(RUNTI ME_CLASS(CDeriv ed1)))
//cast then call
if (myNewBase->IsKindOf(RUNTI ME_CLASS(CDeriv ed2)))
//cast then call
if (myNewBase->IsKindOf(RUNTI ME_CLASS(CDeriv ed3)))
//cast then call
is there a better way to call the Derived Classes Member function?
thanks,

how
Aug 10 '05 #1
2 1988
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(CDerive d1* other);
}

// Derived Class 2
class CDerived1: public CBase
{
void DoStuff(CDerive d1* 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(C Base* MyDerived)
{
CBase* myNewBase = (CBase*)
MyDerived->GetRuntimeClas s()->CreateObject() ;
myNewBase->DoStuff(MyDeri ved);
}

but this always calls CBase::DoStuff( CBase* other);
and I want it to call CDerived1::DoSt uff(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(RUNTI ME_CLASS(CDeriv ed1)))
//cast then call
if (myNewBase->IsKindOf(RUNTI ME_CLASS(CDeriv ed2)))
//cast then call
if (myNewBase->IsKindOf(RUNTI ME_CLASS(CDeriv ed3)))
//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(Derived 1 *pd); // whatever
void DoStuff(Base* pb) { // this one overrides the Base::DoStuff
Derived1* pd1 = dynamic_cast<De rived1*>(pb);
if (pd1)
this->DoStuff(pd1) ;
}
};

class Derived2 : Base {
/* non-virtual */ void DoStuff(Derived 2 *pd); // whatever
void DoStuff(Base* pb) { // this one overrides the Base::DoStuff
Derived2* pd2 = dynamic_cast<De rived2*>(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.publ ic.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.publ ic.vc.language'

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

"Victor Bazarov" <v.********@com Acast.net> wrote in message
news:Hk******** **********@news read1.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(CDerive d1* other);
}

// Derived Class 2
class CDerived1: public CBase
{
void DoStuff(CDerive d1* 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(C Base* MyDerived)
{
CBase* myNewBase = (CBase*)
MyDerived->GetRuntimeClas s()->CreateObject() ;
myNewBase->DoStuff(MyDeri ved);
}

but this always calls CBase::DoStuff( CBase* other);
and I want it to call CDerived1::DoSt uff(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(RUNTI ME_CLASS(CDeriv ed1)))
//cast then call
if (myNewBase->IsKindOf(RUNTI ME_CLASS(CDeriv ed2)))
//cast then call
if (myNewBase->IsKindOf(RUNTI ME_CLASS(CDeriv ed3)))
//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(Derived 1 *pd); // whatever
void DoStuff(Base* pb) { // this one overrides the Base::DoStuff
Derived1* pd1 = dynamic_cast<De rived1*>(pb);
if (pd1)
this->DoStuff(pd1) ;
}
};

class Derived2 : Base {
/* non-virtual */ void DoStuff(Derived 2 *pd); // whatever
void DoStuff(Base* pb) { // this one overrides the Base::DoStuff
Derived2* pd2 = dynamic_cast<De rived2*>(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.publ ic.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
11226
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 generates this warning ... "warning C4311: 'type cast' : pointer truncation from 'void *' to 'DWORD'" The warning is generated as the pointer is apparently 64 bits long (even though a quick sizeof() reports it to only be 4 bytes in length) and...
4
1889
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 other words, should it be Namespace::ObjectName* someObject = (Namespace::ObjectName*)hashMap->Item(key); or Namespace::ObjectName* someObject =
6
6404
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. Of course it first tries to obtain the appropriate TypeConverter. However, for some types there are no applicable type converters. Consider a custom class "Dummy" from a linked assembly (in my scenario, neither the Dummy type nor its assembly...
4
1937
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
3399
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 could type cast right into it. However when I changed back to a class that was derived from C++ I go back to getting the error. When type casting does the object I am casting to have to be derived from the same object as what was on my list ?
44
2234
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
4738
by: James Juno | last post by:
Hello, I want to do something like this: class A { ... }; class B : public A
11
3518
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 casting (explicitly / implicitly). Code looks as below : const double d_a = 100e-9; // 100ns const double d_b = 20e-9; // 20ns
2
1706
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 code works fine when (int(*)(int))toupper is replaced
0
9476
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9263
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9208
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8210
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6751
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4570
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4825
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3279
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2193
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.