What are the dangers of forward declarations?

前端 未结 9 1417
轻奢々
轻奢々 2021-02-01 16:58

I just had an interview. I was asked what is a \"forward declaration\". I was then asked if they were dangers associated with forward declarations.

I could not answer to

9条回答
  •  轻奢々
    轻奢々 (楼主)
    2021-02-01 17:12

    The only danger of forward-declaring something is when you do the forward declaration outside of a header or in a non-shared header, and the signature of the forward declaration differs from the actual signature of whatever being forward-declared. If you do it in extern "C", there would be no name mangling to check the signature at link time, so you may end up with undefined behavior when the signatures do not match.

提交回复
热议问题