How does RTTI work?

This is a discussion on How does RTTI work? within the C++ Programming forums, part of the General Programming Boards category; Hi, just wrote this simple program: Code: #include <iostream> using namespace std; class base { public: virtual ~base() {} virtual ...

  1. #1
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286

    How does RTTI work?

    Hi, just wrote this simple program:

    Code:
    #include <iostream>
    using namespace std;
    
    class base {
    public:
    	virtual ~base() {}
    	virtual void print() {
    		cout << "base" << endl;
    	}
    };
    
    class derived : public base {
    public:
    	void print() {
    		cout << "derived" << endl;
    	}
    };
    
    void print(base* ptr) {
    	ptr->print();
    }
    
    int main() {
    	base *base_ptr = dynamic_cast<base*>(new derived);
    	print(base_ptr);
    	system("PAUSE");
    }
    and the program writes "derived" in the terminal. This is what's called run time type information (RTTI) I guess, since the print function have no way, when compiling, to know wheather ptr is really a base* or not. So how can it determine which type ptr is of in runtime? Information about which type ptr is has to be stored somewhere (hasn't it), but where?
    Come on, you can do it! b( ~_')

  2. #2
    The larch
    Join Date
    May 2006
    Posts
    3,573
    This program writes derived because the print method is virtual. The upcast doesn't need an explicit cast.
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  3. #3
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,719
    This is just normal polymorphism; you do not need dynamic_cast to cast a derived* to a base* since an implicit conversion will do.

    That said, have you tried searching the Web for RTTI? Its implementation is uh, implementation defined.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,239
    Quote Originally Posted by laserlight View Post
    This is just normal polymorphism; you do not need dynamic_cast to cast a derived* to a base* since an implicit conversion will do.

    That said, have you tried searching the Web for RTTI? Its implementation is uh, implementation defined.
    It's implementation-defined, but the obvious implementation (and what most compilers do in practice) is to examine the vtable and associate that to the actual type. This implies that a class must have a virtual method to participate in RTTI.
    Code:
    //try
    //{
    	if (a) do { f( b); } while(1);
    	else   do { f(!b); } while(1);
    //}

  5. #5
    Algorithm engineer
    Join Date
    Jun 2006
    Posts
    286
    Implementation defined, yeah. I guess every object of a polymorphic class will have to contain a pointer to it's dispatch table, is that maybe how it works? Hm, let's see...

    Quote Originally Posted by Wikipedia

    The C++ standards do not mandate exactly how dynamic dispatch must be implemented, but compilers generally use minor variations on the same basic model.

    Typically, the compiler creates a separate vtable for each class. When an object is created, a pointer to this vtable, called the virtual table pointer or vpointer, is added as a hidden member of this object (becoming its first member unless it's made the last). The compiler also generates "hidden" code in the constructor of each class to initialize the vpointers of its objects to the address of the corresponding vtable.
    Yupp!

    Thanks, haven't heard about vtables before!

    -Kristofer
    Come on, you can do it! b( ~_')

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 11:39 AM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM

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