Public Group

Archived

This topic is now archived and is closed to further replies.

Determining a variable's type

This topic is 5480 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Is it possible to determine the type of a variable, something likE:
int    a = 12;
//float  a = 234.4f;

//VECTOR a = VECTOR(0.0f, 1.0f, 0.0f);

// pseudocode

if (a == int)
cout<<"int";
else if (a == float)
cout<<"float";
else if (a == VECTOR)]
cout<<"VECTOR";
else
cout<<"Unknown type";


Share on other sites
Use RTTI - typeof(), IIRC.

Share on other sites
It's typeid, actually.

I would strongly recommend that you read up on RTTI before using it.

Also remember you might be able to use templates (specialisation), i.e. compile-time - *woot*.

Edit: formatting...

[ Google || Start Here || ACCU || STL || Boost || MSDN || GotW || CUJ || MSVC++ Library Fixes || BarrysWorld || E-Mail Me ]

[edited by - Lektrix on September 24, 2003 11:07:51 AM]

Share on other sites
I once thought I needed to do RTTI and looked at it, got code examples and was then warned that it adds quite a bit of overhead,something to do with when the program is compiling. If you have a valid reason for using RTTI then fine, you switch it on somewhere in your project options if I remember correctly, but if possible think why you are doing it before trying it.

[edited by - hammerstein_02 on September 25, 2003 4:05:01 AM]

Share on other sites
Thanks for the info, I don''t really need to do this, I just wanted to try it out to see if it could be done.

Share on other sites
typeid doesnt add any overhead to types that can be evaluated at compile-time, ie int.
When using RTTI (that is, dynamic_cast and typeid on pointers/references to objects in a inheritancee tree) a very small overhead will be added. The type-info would be added to the class'' vtable, thus not doing any overhead at all, providing the class already has a vtable (else, a vptr will be added). But any object of a class thats in a inheritance-tree should have at least one virtual function, and thereby a vptr and vtable, so rtti has very little overhead.

1. 1
2. 2
3. 3
4. 4
Rutin
11
5. 5

• 12
• 19
• 10
• 14
• 10
• Forum Statistics

• Total Topics
632665
• Total Posts
3007711
• Who's Online (See full list)

There are no registered users currently online

×