forward declarations

前端 未结 3 767
失恋的感觉
失恋的感觉 2020-12-20 07:42

Why do I need to use forward declarations for a class when I am already including the relevant header file? It has fixed my problem but confused me!

essentially clas

3条回答
  •  长情又很酷
    2020-12-20 08:16

    A viceversa relation between classes always needs at least one forward declaration. You can define only a class at a time, so when you define the first class the second is undefined so far. Including headers is not enough, because those headers should also be including each others. Including a file is no different than pasting its contents at the include location. With that in mind, how would you declare two classes that refer to each other within a single file? The answer, as you already know, is forward declarations.

提交回复
热议问题