RTTI typeid question

M

Mike

I want to use typeid() in a base class function to determine the name of the
derived class. typeid(this) returns the name of the base class (which is an
abstract class) rather than the derived class. I'd rather not require the
typeid call in every derived class implementation. How do I get around
this?

I'm depending on the statement in Microsoft Visual Studio documentation
which states :

"
typeid( expression )
....
....
If the expression points to a base class type, yet the object is actually of
a type derived from that base class, a type_info reference for the derived
class is the result. The expression must point to a polymorphic type, that
is, a class with virtual functions. "

example

class foo
{
void create();
void virtfunction()=0;
const char *m_type;
};

class x : public foo
{
create();
void virtfunction;
}

void foo::create()
{
const type_info *T = typeid(this); // this produces type
information for foo, not the derived class!!
m_type = T->name();
}

void x::Create()
{
// I'd rather not put the typeid call here although things work if I do
foo::create();
}

void x::virtfunction()
{
// does something
}
 
V

Victor Bazarov

Mike said:
I want to use typeid() in a base class function to determine the name of the
derived class. typeid(this) returns the name of the base class (which is an
abstract class) rather than the derived class. I'd rather not require the
typeid call in every derived class implementation. How do I get around
this?

I'm depending on the statement in Microsoft Visual Studio documentation
which states :

"
typeid( expression )
...
...
If the expression points to a base class type, yet the object is actually of
a type derived from that base class, a type_info reference for the derived
class is the result. The expression must point to a polymorphic type, that
is, a class with virtual functions. "

Your class 'foot' does not have virtual functions.
example

class foo
{
void create();
void virtfunction()=0;

Did you mean to say

virtual void virtfunction()=0;

?
const char *m_type;
};

class x : public foo
{
create();
void virtfunction;

Did you mean to say

void virtfunction();

??
}

void foo::create()
{
const type_info *T = typeid(this); // this produces type
information for foo, not the derived class!!
m_type = T->name();
}

void x::Create()
{
// I'd rather not put the typeid call here although things work if I do
foo::create();
}

void x::virtfunction()
{
// does something
}

Try to always post the _actual_code_ that doesn't work or doesn't
compile, instead of typing some nonsense while in your newsreader.

Victor
 

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,766
Messages
2,569,569
Members
45,042
Latest member
icassiem

Latest Threads

Top