1009.Dynamic_cast

来源:互联网 发布:苏亚雷斯进球数据 编辑:程序博客网 时间:2024/05/22 15:26


Time Limit: 1sec    Memory Limit:256MB
Description
 
Three classes A, B and C are shown below:
 
class A {
public:
    virtual ~A() {};
};
class B: public A {}; 
class C: public B {}; 
 
You are to implement a function string verify(A *), such that it returns "grandpa" if the passed-in argument points to a class A object, and "father" for a class B object , "son" for a class C object.
 
Your submitted source code should include the whole implementation of the function verify, but without any class defined above.
No main() function should be included.

#include<iostream>#include <typeinfo>using namespace std;string verify(A *p){       if(typeid(A).name()==typeid(*p).name()){return "grandpa";}       if(typeid(B).name()==typeid(*p).name()){return "father";}       if(typeid(C).name()==typeid(*p).name()){return "son";}       }                                 


 conclusion:真是一句话搞定。。 具体typeid().name() 以及typeinfo.h的问题会继续学习,毕竟是我目前未知的领域

利用c++ typeid获取类型名 这是十分灵活的用法,其它用法未知,后续补上

Time Limit: 1sec    Memory Limit:256MB
Description
 
Three classes A, B and C are shown below:
 
class A {
public:
    virtual ~A() {};
};
class B: public A {}; 
class C: public B {}; 
 
You are to implement a function string verify(A *), such that it returns "grandpa" if the passed-in argument points to a class A object, and "father" for a class B object , "son" for a class C object.
 
Your submitted source code should include the whole implementation of the function verify, but without any class defined above.
No main() function should be included.

 

0 0
原创粉丝点击