1 / 8

Prepared by N.Sathish Kumar CSE/ Lecturer SKREC

Prepared by N.Sathish Kumar CSE/ Lecturer SKREC. RTTI AND TEMPLATES Class templates work well with RTTI, since all they do is generate classes. As usual, RTTI provides a convenient way to obtain the name of the class . . #include < iostream > #include < typeinfo > using namespace std;

apu
Download Presentation

Prepared by N.Sathish Kumar CSE/ Lecturer SKREC

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Prepared byN.Sathish KumarCSE/ LecturerSKREC

  2. RTTI AND TEMPLATES • Class templates work well with RTTI, since all they do is generate classes. • As usual, RTTI provides a convenient way to obtain the name of the class .

  3. #include <iostream> #include <typeinfo> using namespace std; template<int id> class Announce { public: Announce() { cout << typeid(*this).name() << " constructor" << endl; }

  4. ~Announce() { cout << typeid(*this).name() << " destructor" << endl; } }; class X : public Announce<0> { Announce<1> m1; Announce<2> m2;

  5. public: X() { cout << "X::X()" << endl; } ~X() { cout << "X::~X()" << endl; } }; int main() { X x; }

  6. This template uses a constant int to differentiate one class from another, but type arguments would work as well. • Inside both the constructor and destructor, RTTI information produces the name of the class to print. • The class X uses both inheritance and composition to create a class that has an interesting order of constructor and destructor calls.

  7. The output is Announce<0> constructor Announce<1> constructor Announce<2> constructor X::X() X::~X() Announce<2> destructor Announce<1> destructor Announce<0> destructor Of course, you may get different output depending on how your compiler represents its name( ) information

  8. THANK YOU

More Related