By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,406 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,406 IT Pros & Developers. It's quick & easy.

RTTI typeid question

P: n/a
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
}
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
Mike wrote:
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
Jul 22 '05 #2

P: n/a
Try: typeid(*this)

:)

"Mike" <mi**@somewhere.com> wrote in message
news:boMtc.14888$Ly.14192@attbi_s01...
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
}

Jul 22 '05 #3

P: n/a
Thanks, Carl. That did the trick.

"Carl Ribbegaardh" <ca*********************@hotmail.com> wrote in message
news:2h************@uni-berlin.de...
Try: typeid(*this)

:)

"Mike" <mi**@somewhere.com> wrote in message
news:boMtc.14888$Ly.14192@attbi_s01...
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
}


Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.