Thread: how to uuse 'typeid'

  1. #1
    Registered User
    Join Date
    Sep 2007
    Posts
    119

    how to uuse 'typeid'

    I wanted to use 'typeid' like I would use 'instanceof' in java. What's the easiest and best way to facilitate this in C++? Also, does typeid( parameter ).name() return a mangled string of some sort, or the actual clear name of the class to use for a means of comparison? Should I just declare an arbitrary object of a certain type, and use it as a means of comparison of my current object? Any help would be great.

    Code:
    In this example, value can be any class in a hiearchy I am working with
    
    Arbitrary x;
    
    if( typeid( value ) == typeid( x ) ) //check if value is of type x (Arbitrary).  Or would I have to use .name() for both of them?
    
    if( typeid( value ).name() == "Arbitrary" ) or if( typeid( value ).name() == Arbitrary ) //Check for string comparison, for whichever one is the proper way of coding it

  2. #2
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    The first one is correct:
    if ( typeid(Arbitrary) == typeid(x) )
    Would be true in this.
    const_info::name returns a "human readable" string for the representation of the type. I don't know if it's the same across compilers so I'd advise against using it for detecting type.
    Also note that you can't compare a string like in your second example, because it returns const char*.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  3. #3
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    The format of the name() return value is not standardized. Some compilers return the mangled name, some the unmangled form, and the only guarantee is that typeid(x) == typeid(y) implies typeid(x).name() == typeid(y).name() and vice versa. (Using a conceptual == - as Elysia said, they're C strings and must be compared with strcmp.)
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Recursive Tree Traversal
    By John_L in forum C++ Programming
    Replies: 12
    Last Post: 02-17-2008, 08:34 AM
  2. typeid problem
    By mikahell in forum C++ Programming
    Replies: 13
    Last Post: 12-03-2006, 06:35 PM
  3. Debug function using typeid?
    By screemfead in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2006, 10:42 AM
  4. typeid and the type_info object
    By major_small in forum C++ Programming
    Replies: 1
    Last Post: 08-24-2004, 11:01 AM
  5. Typeid and Templates
    By Trauts in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2003, 08:55 PM