What are the dangers of forward declarations?

前端 未结 9 1418
轻奢々
轻奢々 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:20

    Another danger of forward declarations is that it makes it easier to violate the One Definition Rule. Assuming that you have a.h forward declaring class B (which is supposed to be in b.h and b.cpp), but inside a.cpp you actually include b2.h which declares a different class B than b.h, then you get to undefined behaviour.

提交回复
热议问题