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
}
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
}