473,385 Members | 2,004 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,385 software developers and data experts.

why the RTTI cannot tell the const qualifier of variables

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 << typeid(pc).name() << endl;
cout << typeid(pcc).name() << endl;
cout << typeid(cpc).name() << endl;

in vc6, I got

char
char
char *
char const *
char const *

and in gcc, I got

c
c
Pc
PKc
PKc
Jul 22 '05 #1
3 3032
Zeng Dinghao wrote:
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 << typeid(pc).name() << endl;
cout << typeid(pcc).name() << endl;
cout << typeid(cpc).name() << endl;

in vc6, I got

char
char
char *
char const *
char const *


This is the expected result. The C++ standard explicitly
says:

The top-level cv-qualifiers of the lvalue expression or the type-id that is the
operand of typeid are always
ignored. [Example:
class D { ... };
D d1;
const D d2;
typeid(d1) == typeid(d2); // yields true
typeid(D) == typeid(const D); // yields true
typeid(D) == typeid(d2); // yields true
typeid(D) == typeid(const D&); // yields true
—end example]

There are practical reasons for this. top-level constness is a
rather fleeting thing.

Jul 22 '05 #2
Ron Natalie wrote:
typeid(D) == typeid(const D&); // yields true
—end example]

Ooops...this is either wrong or the standard is....better
go call this one up in comp.std.c++.
Jul 22 '05 #3
Zeng Dinghao wrote:
char c = 'd';
const char cc = 'd';
char* pc = "dfdfdf";
const char* pcc = "dfdfdf";
char const* cpc = "dfdf";
...
Subject: why the RTTI cannot tell the const qualifier of variables


Because 'typeid' does not work with types of variables. 'typeid' works
with types of expression results.

In C++ language const-qualification is not applicable to rvalues of
non-class type. Such rvalues are neither const nor non-const, they are
simply rvalues. For example, the result of the expression 'cc' is an
rvalue of type 'char' (not 'const char').

For this reason, if you apply 'typeid' to an expression, which returns
an rvalue of non-class type, you'll never see any traces of
const-qualification in the result of 'typeid'.

--
Best regards,
Andrey Tarasevich
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Mahesh Tomar | last post by:
Dear Readers, I am porting my existing C code to C++. In my existing code there are numerous functions that has been defined with CONST qualifier. For eg. foo(const DATA_TYPE *x); DATA_TYPE is...
7
by: Andrej Prsa | last post by:
Hello, everyone! When a const int * argument is passed to a function, i.e. int f (const int *var) { printf ("%d\n", *var); } int main ()
3
by: Steven T. Hatton | last post by:
I'm trying to work out a design for dynamically determining file types, and for generating new files of a given type. I'm curious to know what others think of my current strategy. Is it "so...
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 }; ...
4
by: junky_fellow | last post by:
Hi guys, Consider the following statements. const int *ptr; /* ie the contents pointed to by ptr cannot be changed */ My question is how/who prevents the contents from being modified ?...
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 , ...
3
by: Chameleon | last post by:
What is better if you want upcasting in intermediate classes like below? Multiple Inheritance and Overloading or simply RTTI? RTTI wants time but MI and Overloading create big objects (because of...
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...
9
by: istillshine | last post by:
It seems I never need to use it. It only made a long line longer, and made you type five more characters. I feel so strange why people are talking about it.
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
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...

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.