Question

namespace std {
 class type_info
 {
 public:
  virtual ~type_info(); //type_info can serve as a base class
 // enable comparison
  bool operator==(const type_info& rhs ) const;
 // return !( *this == rhs)
  bool operator!=(const type_info& rhs ) const;
  bool before(const type_info& rhs ) const; // ordering
 //return a C-string containing the type's name
  const char* name() const;
 private:
  //objects of this type cannot be copied
     type_info(const type_info& rhs );
     type_info& operator=(const type_info& rhs);
 }; //type_info
}  

In the declaration of type_info class ,I can't find any data member .So what is constructed or destructed ?? Also typeid isn't declared in it.So how type_info object is accessed by it?
Is above representation incomplete? Please tell about the type of data member in type_info class

Was it helpful?

Solution

It looks like you are looking at the public interface of typeinfo from C++03. The standard doesn't restrict an implementation from adding members to a standard class (so long as there names come from those reserved to the implementation) to make things work.

In the implementation that I am currently using std::typeinfo has a private member const char* __name which is used to implement the public member functions according to the requirements of the standard.

ISO/IEC 14882:2011 17.5.2.3 Private members [objects.within.classes] / 1:

Clauses 18 through 30 and Annex D do not specify the representation of classes, and intentionally omit specification of class members (9.2). An implementation may define static or non-static class members, or both, as needed to implement the semantics of the member functions specified in Clauses 18 through 30 and Annex D.

Similar wording appears in C++03 17.3.2.3.

OTHER TIPS

typeid is a keyword, not a function, so it need not and cannot be declared. It cannot be a function, since then the expression typeid(T) where T is a type would not be valid. It is implemented in the compiler, not in the library.

typeinfo doesn't need to have any data members; its name method is allowed to serve strings from what is effectively a static array of them. For example, consider this simple class that knows its own name:

// foo.h
class Foo {
    // look ma, no data members!
  public:
    char const *name() const;
};

// foo.cpp
char const NAME[] = "Foo";
char const *Foo::name() const { return NAME; }

Apart from name, the main operation on typeinfo objects is ==, which can be implemented by a simple pointer comparison between the objects; in that case, the compiler could construct a big typeinfo [] array somewhere private and typeid would just index into that array to fetch the right typeinfo object. How it works, however, is implementation-dependent.

There are no data members you can access. type_info is implemented for you by the compiler; the details are not public. You obtain a type_info by using typeid.

Don't worry, it's the compiler's job to make it all work.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top