473,419 Members | 1,925 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

RTTI typeid question

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
3 3720
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steven Lien | last post by:
Hi all As far as i know, there has 2 ways RTTI in C++ one is dynamic_cast and another is typeid Since, my book only pointed me that to use "typeid" and "static_cast" conjunction will be much...
3
by: tirath | last post by:
Hi all, I have a templated class that derives from a non-templated abstract class. How do I then cast a base class pointer to a <templated> derived class pointer in a generalised fashion? ...
9
by: Maurice Termeer | last post by:
Hi, suppose I have an abstract base class called shape and lots of subclasses like square, circle, triangle and such. These subclasses may have subclasses themselves as well, but shape is on top of...
3
by: Zeng Dinghao | last post by:
char c = 'd'; const char cc = 'd'; char* pc = "dfdfdf"; const char* pcc = "dfdfdf"; char const* cpc = "dfdf"; cout << typeid(c).name() << endl; cout << typeid(cc).name() << endl; cout <<...
2
by: Generic Usenet Account | last post by:
My C++ compiler (gcc 3.3.1) is prefixing a number in front on a user-defined structure when I invoke typeid() on it. Can anyone explain why? Source code follows: Bhat ...
4
by: Derek | last post by:
What happens if I invoke typeid on an object that's part of a library that was compiled w/o RTTI enabled? Is this undefined behavior? If not, is a bad_typeid exception what I should expect? ...
33
by: mscava | last post by:
Well I've got a problem, that is more theoretical than practital. I need to know benefits of RTTI. I see another way of doing it... class A { public: ~virtual A() {} enum Type { X, Y, Z }; ...
6
by: William | last post by:
for example, I have a global object: extern Object myobj; Can gcc get this object by using string "myobj" at runtime?? I know C++ rtti doesnt support this, but I dont know if gcc can , ...
6
by: Generic Usenet Account | last post by:
I ran a small experiment involving RTTI and dynamic casting. Basically what I did was to cast a base class pointer to a derived type (yes, I know that is not kosher). I then performed...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.