www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - cast a LinkSeq

reply Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Hi All,

can I cast a LinkSeq from inherited type to base type?

------------------------ code --------------------------
   class Fruit {}
   class Apple: Fruit {}

   auto apples = new LinkSeq!(Apple);
   apples.append(new Apple);
   assert(apples !is null);
   assert(apples.length == 1);
   
   auto fruits = cast(LinkSeq!(Fruit))(apples);
   assert(fruits !is null); // <--- failed
   assert(fruits.length == 1);   
------------------------ code --------------------------


--Qian
Apr 06 2009
parent Qian Xu <quian.xu stud.tu-ilmenau.de> writes:
Qian Xu wrote:
 Hi All,
 
 can I cast a LinkSeq from inherited type to base type?
What I can do, is to create a new LinkSeq!(Fruit) and then cast all elements one by one. -- Xu, Qian (stanleyxu) http://stanleyxu2005.blogspot.com
Apr 06 2009