What are the dangers of forward declarations?

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

    A forward declaration is not so much dangerous in itself, but it is a code smell. If you need a forward declaration, it means two classes are tightly coupled, which usually is bad. Thus it is an indication that your code may need refactoring.

    There are some cases where it is OK to have tight coupling, e.g. the concrete states in a state pattern implementation may be tightly coupled. I would consider this OK. But in most other cases, I would improve my design before using a forward declaration.

提交回复
热议问题