Thread: What are the restrictions on typeid?

  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
    5,108
    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
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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,895
    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, 07: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, 03:23 AM