What will be the output of the following C++ code? [code] #include <iostream> #include <typeinfo> using namespace std;

Nam Phong

New member
What will be the output of the following C++ code?
Code:
    #include <iostream>
    #include <typeinfo>
    using namespace std;
    class Polymorphic {virtual void Member(){}};
    int main () 
    {
        try
        {
            Polymorphic * pb = 0;
            typeid(*pb);   
        }
        catch (exception& e)
        {
            cerr << "exception caught: " << e.what() << endl;
        }
        return 0;
    }
A. exception caught: std::bad_typeid
B. exception caught: std::bad_alloc
C. exception caught: std::bad_cast
D. exception caught: std::bad_id
 
Answer: a
Explanation: In this program, We used a bad type id for the polymorphic operator, So it is arising an bad_typeid exception.
Output:
Code:
$ g++ etae.cpp
$ a.out
exception caught: std::bad_typeid
 

Tìm kiếm

Back
Top