How do I cause a conversion from base class?

  • Thread starter Thread starter Guest
  • Start date Start date
G

Guest

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
 
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
 
Back
Top