[問題] Access derived class from base class

作者: ScottOAO (cos)   2015-05-20 08:12:36
假設今天有
class creature
class animal : creature
class plants : creature
然後陣列可能存animal 或plants
creature[] c;
c[0] = new animal();
c[1] = new plants();
但是我要如何判斷陣列中存的是何種derived class?
有沒有不用dynamic的做法? 謝謝
補問一下,強制cast好嗎?
作者: ssccg (23)   2015-05-20 09:06:00
c[0].getType()要判斷的話用 C[0] is Animal、c[0] is Plants
作者: Litfal (Litfal)   2015-05-20 09:19:00
先想想能不能用多型解決

Links booklink

Contact Us: admin [ a t ] ucptt.com