HP aC++/HP C A.06.28 Programmer's Guide Integrity servers (769150-001, March 2014)
Using namespace directives can be a powerful means to migrate code to libraries that use
namespaces. Occasionally, however, they may silently make unwanted names visible. It is therefore
often suggested not to use using-directives unless the alternatives are very inconvenient.
#include <stdio.h>
namespace N {
char const* f() { return “N::f()”; }
char const* f(double) { return “N::f(double)”; }
char const* g() { return “N::g()”; }
}
char const* g(double) {
using N::f; // Declare all f’s in namespace N
return f(2.0);
}
namespace M { // Illustrate how using-directives
using namespace N; // are transitive
}
int main() {
using namespace N;
printf(“Calling: %s.\n”, f()); // calls N::f()
printf(“Calling: %s.\n”, g(1.0)); // calls ::g(double)
// which calls
// N::f(double)
printf(“Calling: %s.\n”, N::g()); // calls N::g()
printf(“Calling: %s.\n”, M::f()); // calls N::f()
return 0;
}
typeid Keyword
The typeid keyword is an operator, called the type identification operator, used to access type
information at runtime. The operator takes either a type name or an expression and returns a
reference to an instance of type_info, a standard library class.
Usage
You can use runtime type identification when you need to know the exact type of an object. This
might, for example, be necessary to find the name of the object class for diagnostic output. It also
might be used to perform some standard service on an object such as via a database or I/O
system.
typeid Example
Following is an example of the typeid keyword:
# include <iostream.h>
# include <typeinfo>
class Base {
virtual void f(); // Must have a virtual function
// to be a polymorphic type.
// additional class details omitted
};
class Derived : public Base {
// class details omitted
};
void Base::f()
{
146 Standardizing Your Code