Run-Time Checking of a Cast from a void*

前端 未结 3 1444
不思量自难忘°
不思量自难忘° 2021-01-19 12:21

Say that I have a void* containing a pointer to an unknown class. I want to use dynamic_cast to do run-time checking on the type of cl

3条回答
  •  生来不讨喜
    2021-01-19 13:04

    As I understand it you want a polymorphic object but no common base class.

    There is already a fairly standard idiom for this - it's called boost::any.

    A boost::any carries your object plus some type information. The interface allows you to query the type and to attempt to cast the any to the type you're looking for.

    http://www.boost.org/doc/libs/1_59_0/doc/html/any.html

提交回复
热议问题