What are the restrictions on typeid?

This is a discussion on What are the restrictions on typeid? within the C++ Programming forums, part of the General Programming Boards category; Is it guaranteed in all c++ runtimes to give a valid value if the class has at least one virtual ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    What are the restrictions on typeid?

    Is it guaranteed in all c++ runtimes to give a valid value if the class has at least one virtual method? What if one of its base classes has a virtual method but it overrides it without the virtual keyword?

  2. #2
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    4,217
    Is it guaranteed in all c++ runtimes to give a valid value if the class has at least one virtual method?
    It is no more guaranteed to work than any other aspect of C++. Compiler vendors are weird, but if they claim conformance then 'typeid' will probably behave in such a manor. Realize though that what 'typeid' "returns" may or may not be valid even between different runs of the same program.

    What if one of its base classes has a virtual method but it overrides it without the virtual keyword?
    Using the 'virtual' keyword to override a virtual method in a derived class is strictly notational for the sake of the programmer.

    Soma

  3. #3
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by 6tr6tr View Post
    Is it guaranteed in all c++ runtimes to give a valid value if the class has at least one virtual method? What if one of its base classes has a virtual method but it overrides it without the virtual keyword?
    In answer the the first, it is not guaranteed, because RTTI can be turned off at compile time.

    In answer to the second, an overridden virtual method is always virtual, even if the keyword "virtual" is left out. The presence of the keyword in derived classes serves only as a reminder to the programmer that the method is (still) virtual.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    The restrictions are, if you're using it, it's more than likely that your design is buggered.

  5. #5
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    typeid will always give a valid value, unless your compiler is buggy. That doesn't necessarily mean it's a correct value, though. It depends on your view of "correct".
    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. how to uuse 'typeid'
    By John_L in forum C++ Programming
    Replies: 2
    Last Post: 02-15-2008, 06:44 PM
  2. Debug function using typeid?
    By screemfead in forum C++ Programming
    Replies: 3
    Last Post: 09-22-2006, 10:42 AM
  3. typeid and the type_info object
    By major_small in forum C++ Programming
    Replies: 1
    Last Post: 08-24-2004, 11:01 AM
  4. Typeid and Templates
    By Trauts in forum C++ Programming
    Replies: 6
    Last Post: 04-09-2003, 08:55 PM
  5. Edit/Rich Edit window size restrictions
    By Echidna in forum Windows Programming
    Replies: 2
    Last Post: 01-19-2002, 02:23 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21