Re: help with typeid en c++builder6
efrenba wrote:
The problem is in the for statement, I need to identify each elements
of this array as TC1 or TC2 but it doesn't work, the typeid().name()
always returns "TBase".
class __rtti TBase
{
private:
int aAge;
public:
TBase(int pAge = 0) : aAge(pAge) {};
};
TBase *base[3];
TC1 c1('F', 51);
TC2 c2(9.5, 25);
base[0] = &c1;
base[1] = &c2;
base[3] = &c1;
base[3] is out of range!!! Did you test your example? Anyway, I assume you
intended base[2].
for (int i = 0; i < 3; i++)
ShowMessage(typeid(*base[i]).name());
If you want typeid to get you the type_info of the most derived object
(either TC1 or TC2), you have to make sure that TBase is a polymorphic
class. So TBase needs to have at least one virtual function. (In general
the destructor would be a good candidate!)
I see you're using a Borland specific keyword, "__rtti", to get runtime type
identification. Is that really necessary for you, to get things running on
Borland? (It is non-standard!) Anyway, you really need to make sure that
TBase is a polymorphic class!
Regarding "__rtti":
http://docs.codegear.com/products/rad_studio/radstudio2007/RS2007_helpupdates/HUpdate4/EN/html/devwin32/__rtti_xml.html
HTH, Niels
--
Niels Dekker
http://www.xs4all.nl/~nd/dekkerware
Scientific programmer at LKEB, Leiden University Medical Center