Thread: How does RTTI work?

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    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, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM