How often have you had to build a dynamic type framework into an application? It's pretty common in C++ and the choice is either to have 'type cookies' floating around your entire application and doing things like:

	if (p->getCookie() == FrameCookie)
	{
		Frame* fp = (Frame*)p;
		// ...
	}

or, more intrusively, one of these two forms:

	if (p->isFrame())
	...

	Frame* fp = p->getFrame();
	if (fp)
	...

The worst part is that every library that provides such a facility does it in a different way so you can't use objects from one library with methods from another library. So the committee standardised something like the third form:

	Frame* fp = dynamic_cast<Frame*>(p);
	if (fp)
	...

However, this still separates the cast from the test so there is room for mistakes. An additional change solved that problem:

	if (Frame* fp = dynamic_cast<Frame*>(p))
	{
		// fp in scope and not null
	}
	// fp no longer in scope

Here, a variable is declared in a conditional expression and the value of the expression is the initialised value of the variable. The variable remains in scope only for the duration of the conditional so you cannot accidentally use the value without testing it.


Some additional machinery is provided to support runtime type identification: a type enquiry operator and a type description object.

The type enquiry operator is typeid and it returns a reference to a type description object of type type_info:

	if (typeid(*p) == typeid(Frame))
	{
		// it's a Frame pointer
	}

	const type_info&	tir = typeid(*p);
	cout << "p is a pointer to " << tir.name() << '\n';

Not much is defined about the type_info object: it has a name and you can test whether it is equal to another type_info object. The format of the name is not defined (although the committee looked at that in some detail) and there may (or may not) be extended type information available.