能不能判断一个指针的类型
答案:1 悬赏:20 手机版
解决时间 2021-02-07 10:18
- 提问者网友:放下
- 2021-02-07 02:26
能不能判断一个指针的类型
最佳答案
- 五星知识达人网友:何以畏孤独
- 2021-02-07 04:03
稍难一些。c++ 中 有个 typeid() "运算符“, 在运行时,利用
”Runtime type information,RTTI“ 来识别出 变量 的 类型信息,例如是什么型的变量,或是什么 class 的, 什么 结构的。
例如:(不是完整程序,只是关键语句)
Person person;
Employee employee;
Person* ptr = &employee;
Person& ref = employee;
// The string returned by typeid::name is implementation-defined
std::cout << typeid(person).name() << std::endl;
// Person (statically known at compile-time)
std::cout << typeid(employee).name() << std::endl;
// Employee (statically known at compile-time)
std::cout << typeid(ptr).name() << std::endl;
// Person* (statically known at compile-time)
std::cout << typeid(*ptr).name() << std::endl;
”Runtime type information,RTTI“ 来识别出 变量 的 类型信息,例如是什么型的变量,或是什么 class 的, 什么 结构的。
例如:(不是完整程序,只是关键语句)
Person person;
Employee employee;
Person* ptr = &employee;
Person& ref = employee;
// The string returned by typeid::name is implementation-defined
std::cout << typeid(person).name() << std::endl;
// Person (statically known at compile-time)
std::cout << typeid(employee).name() << std::endl;
// Employee (statically known at compile-time)
std::cout << typeid(ptr).name() << std::endl;
// Person* (statically known at compile-time)
std::cout << typeid(*ptr).name() << std::endl;
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯