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

printing variable type at run time

175 100+
I'm having a problem remembering how to get the type of a variable at run time. For example, lets say I have a char* i, and at some point during execution, it is converted to an int. What I want is a function so I can output i's type during execution ( so it will print to the console and I'll see it ).

I did this a few weeks ago, but I can not remember what the function is that does this. I searched around, and found typeid, but either I'm using it wrong, or that wasn't it. If anyone can tell me the function I'm looking for I would really appreciate it.
Nov 29 '07 #1
4 1646
It looks like today everyone is interested in reflection.
As you just wrote,typeid should work;
correct syntax is: typeid(variable).name();
please send us your code so we can examine together the possible errors.
Nov 29 '07 #2
Expand|Select|Wrap|Line Numbers
  1. #include <iostream>
  2. #include <typeinfo>
  3.  
  4. using namespace std;
  5. int main ()
  6.   {
  7.      int a = 5 ;
  8.      double b = 6.999998;
  9. cout << typeid(a).name() << endl;
  10. cout << typeid(b).name() << endl; 
  11.    return 0;
  12.   }
  13.  
it prints:
i
d
Nov 29 '07 #3
manontheedge
175 100+
I feel like an idiot now. I was using the variable "i" and it looked like the variable name was being printed. I guess it's early. Anyway, thank you for the help. I was posting my code when I saw your output, and realised my mistake.
Nov 29 '07 #4
weaknessforcats
9,208 Expert Mod 8TB
I assume you are using typeid for debugging since using it is poor porogramming practice. Assuming that you are debugiing, you should be using your debugger rather than relying on console displays.

typeid requires RTTI and that reuires the compiler to keep a database for every variable in the program on the off-chance you will typeid the thing. This is very expensive in terms of machine resources.
Nov 29 '07 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: google | last post by:
I would like to print a table, using a while loop to insert as many blank text fields as players specified by the user. How do I put html inside a php loop. This is what I have.... <body>...
9
by: Jody Gelowitz | last post by:
I am trying to find the definition of "Safe Printing" and cannot find out exactly what this entitles. The reason is that I am trying to print contents from a single textbox to no avail using the...
1
by: NickB | last post by:
Please could someone tell me what is wrong. Ther error is: An unhandled exception of type 'System.NullReferenceException' occurred in microsoft.visualbasic.dll Additional information: Object...
3
by: Jason James | last post by:
Hi all, I have an application that needs to print numerous documents of different styles (either to the printer or print preview dialog). I have selected which document type to print by...
6
by: Bill | last post by:
Hi I am trying to get my listbox items to print if they stream past the one page mark. my code is working for one page of information (if the e.hasmorepages) is not there. But I am having...
6
by: Siv | last post by:
Hi, I am getting into printing with VB.NET 2005 and want to implement the usual capability that a user can select a selection of pages. I have a report that is generated by my application that if...
8
by: Rick Lederman | last post by:
I am using a PrintDocument and PrintDialog to print. The first time that I print it works, but when I try to print a second time without exiting the entire program I get an...
2
by: David | last post by:
Hi list. I've never used unicode in a Python script before, but I need to now. I'm not sure where to start. I'm hoping that a kind soul can help me out here. My current (almost non-existant)...
0
it0ny
by: it0ny | last post by:
Hi guys, thanks I am fairly new to this forum so I hope I chose the right place to post this question. I try to make my program printout a deposit's report. I created a class to store the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
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
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
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...

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.