Does RTTI mean Dynamic Binding?

779 views Asked by At

In C++, does Run-Time Type Information (RTTI) mean dynamic binding?

3

There are 3 answers

0
dthorpe On BEST ANSWER

RTTI means additional information is included in the compiler output so that runtime code can know details about the source code classes and types that would normally be thrown away in compilation.

For example, the machine code doesn't need to know the name of a function in order to call a function - calling a function in machine code only needs to know the address of the target function.

Another example: The compiled machine code doesn't need to know the name of a class type at runtime. But if you want to build an automatic serialization library, you'll probably want to know the text name of each class so you can write it to the output stream. The class name is RTTI.

If by dynamic binding you mean the ability to find and invoke methods or properties by string name at runtime that were not known at compile time, then yes, RTTI is a resource that can be used for that purpose.

1
tpdi On

RTTI means determining the dynamic type of a reference (or pointer) at runtime:

class Base {};
class Dervied : public Base {};

Base* b ; // Static type of pointer is Base*;
b = new Base(); // Dynamic type (pointed to) is Base;
b = new Derived()' // Dynaic type pointed to is Derived.

RTTI is figuring out what type b actually points to, that is, the dynamic type of b.

RTTI is expensive and it's bad form.

With public inheritance, a Derived is-a Base, so a function ought not to have to care about the dynamic type; anything it can do to anything pointed to by b ought to be OK whether b points to Base or to Derived.

If your function needs to know the dynamic type, it suggests that you're doing something external to Base, that ought to be encapsulated in base, as a method you can call on Base.

If you really need to do something external (and you can't do it, say, with a Visitor, because the external thing is multi-dispatch), then at least encapsulate (the salient) paet of RTTI. Let's say what's salient is simply whether it's Base or not:

class Base { 
  virtual bool isBase() { return true; } 
};

class Dervied : public Base {
  virtual bool isBase() { return false; }
}

If what's salient is the type, do something like this:

class Base { 
  virtual string typeName { return "Base"; } 
};

class Dervied : public Base {
  virtual string typeName { return "Derived"; }
}

Naturally, since I've added a virtual function, I need to add a virtual dtor, which means (Rule of Three) I should also add a copy ctor and copy assignment operator. These are not shown.

0
Marcelo Cantos On

No. Dynamic binding is a fairly loose term referring to the idea of deciding at run-time which of a set of functions to invoke, based on some context. It is usually decided by the actual type of an object via a pointer to a base class, which is commonly know as virtual member functions.

RTTI is a mechanism for discovering the type_info object for an object at runtime. While the two concepts are somewhat related, they are nevertheless quite distinct from each other.