How to write own dynamic_cast

后端 未结 4 1500
失恋的感觉
失恋的感觉 2021-01-13 01:54

This have been asked in the interview.

How to write own dynamic_cast. I think, on the basis of typeid\'s name function.

Now how to implement own typid? I hav

4条回答
  •  Happy的楠姐
    2021-01-13 02:23

    To attempt an answer slightly less routine, if slightly less defined:

    What you need to do is cast the pointer to an int*, create a new type T on the stack, cast a pointer to it to int*, and compare the first int in both types. This will do a vtable address comparison. If they are the same type, they will have the same vtable. Else, they don't.

    The more sensible of us just stick an integral constant in our classes.

提交回复
热议问题