help with typeid en c++builder6

E

efrenba

Hi,

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".



#include <typeinfo.h>

class __rtti TBase
{
private:
int aAge;

public:
TBase(int pAge = 0) : aAge(pAge) {};
};

class TC1: public TBase
{
private:
char aSex;

public:
TC1(char pSex = 'F', int pAge = 0) : aSex(pSex), TBase(pAge) {};

char getSex() const { return aSex; };
};

class TC2: public TBase
{
private:
float aMoney;

public:
TC2(float pMoney = 0.0, int pAge = 0) : aMoney(pMoney), TBase
(pAge) {};

float getMoney() const { return aMoney; };
};

------------------

TBase *base[3];

TC1 c1('F', 51);
TC2 c2(9.5, 25);

base[0] = &c1;
base[1] = &c2;
base[3] = &c1;

for (int i = 0; i < 3; i++)
ShowMessage(typeid(*base).name());
 
V

Victor Bazarov

efrenba said:
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".
[..]
------------------

TBase *base[3];

TC1 c1('F', 51);
TC2 c2(9.5, 25);

base[0] = &c1;
base[1] = &c2;
base[3] = &c1;

for (int i = 0; i < 3; i++)
ShowMessage(typeid(*base).name());


Nothing. Given 'TBase *p;', 'typeid(*p)' will always be 'TBase'.
'typeid' is a compile-time operator, it cannot know what dynamic type
might be behind that pointer.

V
 
N

Niels Dekker - no reply address

efrenba said:
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).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/r...tes/HUpdate4/EN/html/devwin32/__rtti_xml.html

HTH, Niels
 
T

Thomas J. Gritzan

Victor said:
efrenba said:
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".
[..]
------------------

TBase *base[3];

TC1 c1('F', 51);
TC2 c2(9.5, 25);

base[0] = &c1;
base[1] = &c2;
base[3] = &c1;

for (int i = 0; i < 3; i++)
ShowMessage(typeid(*base).name());


Nothing. Given 'TBase *p;', 'typeid(*p)' will always be 'TBase'.
'typeid' is a compile-time operator, it cannot know what dynamic type
might be behind that pointer.


Wrong. If TBase is a polymorphic class, that means if it has one or more
virtual functions, then typeid(*p) will result to the type_info
referring to the dynamic type of *p.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top