Click here to Skip to main content
15,880,392 members

Down cast in a derived hierarchy

Revision 2
Hi people, I've ran into a problem where i wanna downcast a object of a different type to a object longer down in a derived hierarchy, i think the best way is just to show some simple code:

C#
class mammal {}
class dog : mammal{}
class cat : : mammal{}
class program
{
   public main()
   {
      mamal m = new mamal();
      cat c = new cat();
      c = (cat)m; //this give me an invalid cast exception!
      c = (m as cat); //same exception as above
   }
}


as seen i wanna give a cat object a mammal object/have all the info in the mammal putted into the cat object, which is derived from mammal, i tried implicit operators but saw that this is invalid to do in derived classes. so is there a way to do this without doing everything manually?

thanks - Jackie
Posted 16-Nov-12 12:06pm by Jackie00100.
Tags: ,