473,396 Members | 1,748 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,396 software developers and data experts.

How do I cause a conversion from base class?

If I have a series of classes let's say B, C, and D that all derive from
class A and each of these classes can convert from/to each other. C knows
how to convert from D and B into a C and likewise for the others.

Now, I am given an object that is one of the above types but I am given it
as an A.

Now I know that I can do something like this:
if (a is B) { return (C)(a as B); }

but in my case I would have lots of the if (a is <sometype>) since I have
several of these types. I'm looking for a fast way to convert from one type
to another type when all I have is a reference to a base class.

Thanks
Reggie
Nov 16 '05 #1
1 1267

is/as is probably your best bet. You can do a raw recast "D = (D) A" but
that will throw an exception if A is not a D. The exception is not nearly as
efficient execution wise as a series of is/as checks...

--Richard

"Reggie" wrote:
If I have a series of classes let's say B, C, and D that all derive from
class A and each of these classes can convert from/to each other. C knows
how to convert from D and B into a C and likewise for the others.

Now, I am given an object that is one of the above types but I am given it
as an A.

Now I know that I can do something like this:
if (a is B) { return (C)(a as B); }

but in my case I would have lots of the if (a is <sometype>) since I have
several of these types. I'm looking for a fast way to convert from one type
to another type when all I have is a reference to a base class.

Thanks
Reggie

Nov 16 '05 #2

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

Similar topics

4
by: Jim Hubbard | last post by:
I have some C# code that is supposed to wrap the defrag APIs and I am trying to convert it to VB.Net (2003). But, I keep having problems. The C# code is relatively short, so I'll post it...
5
by: pw4getter | last post by:
Is there any rationale to prohibit implicit conversion from derived** to base** ? To illustrate my question I submit couple lines below: class base { public: base(){} virtual ~base(){} };...
6
by: Ryan Mitchley | last post by:
Hi all Given bool bResult; shared_ptr<cSampleData> pNewData; shared_ptr<cBase> pNewBase; where cSampleData is descended from cBase, the following gives me a valid pNewData to the correct...
2
by: frantz | last post by:
Maybe a stupid question: ------------------------ Is there ihneritance for conversion operator? If yes or no, what is the reason(s)... Thanks for your help. Example: -------- class A {
12
by: Vladimir_petter | last post by:
Dear All, There is the problem in nutshells (see the program bellow): It is ok to convert pointer to F<T> to the pointer to I. Now if I have pointer to member "F<T> entity::*" can I convert it...
2
by: Harold Howe | last post by:
Howdy all, I am getting a compiler error regarding a consrained conversion. It complains that it can't make the type conversion, even though the generic type argument inherits from the target of...
10
by: Michael D. Ober | last post by:
I have a class: Private Class ClientList Inherits StringCollection Public InternalUser As Boolean = False Public Shadows Function Contains(ByVal ClientNumber As String) As Boolean
11
by: jyck91 | last post by:
// Base Conversion // Aim: This program is to convert an inputted number // from base M into base N. Display the converted // number in base N. #include <stdio.h> #include <stdlib.h>...
6
by: Peter Lee | last post by:
what's the correct behaver about the following code ? ( C++ standard ) I got a very strange result.... class MyClass { public: MyClass(const char* p) { printf("ctor p=%s\n", p);
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: 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
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...
0
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,...

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.