
A "forward declaration" is a declaration of an entity without an associated definition.
“前向聲明”是沒有關聯定義的實體聲明。
//?A.h
#include?"B.h"
class?A?{?B?b;?};
//?B.h
#include?"A.h"
class?B?{?A?a;?};
//?b.h:
struct?B?{};
struct?D?:?B?{};
//?good_user.cc:
#include?"b.h"
void?f(B*);
void?f(void*);
void?test(D*?x)?{?f(x);?}??//?Calls?f(B*)
若把#include換成前置聲明,由于聲明時不知道D是B的子類,test()中f(x)就會導致f(void*)被調用,而不是f(B*)。
盡可能避免使用前向聲明。相反,請包含所需的頭文件。
https://google.github.io/styleguide/cppguide.html#Forward_Declarations
END
點贊、轉發加關注,一鍵三連,好運年年
關注公眾號后臺回復數字688可獲取嵌入式相關資料
往期推薦
C語言中的不完整類型

為什么C語言執行效率高,運行快?

二維數組與二級指針是好兄弟嗎?

C語言實現最小二乘法

