What are the dangers of forward declarations?

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

    Forward declaration is the symptom of C++ missing modules (going to be fixed in C++17?) and using headers inclusion, if C++ had modules there were no need at all for forward declarations.

    A forward declaration is not less than a "contract", by using it you actually promise that you will provide the implementation of something (after in the same source file, or by linking a binary later).

    The cons of that is that you actually have to follow your contract, not much very an issue because if you don't follow your contract the compiler will somehow complain early, but in some languages code just get executed without the need to "promise its own existence" (speaking of dynamically typed languages)

提交回复
热议问题