我們想要的名字混淆一些情況,並且可以決定使用using
或define
,例如:應該用優於定義名稱混淆的C++
using ComponentRef = std::shared_ptr<Component>;
#define ComponentRef std::shared_ptr<Component>
是有一些原因,其中之一是首選在另一個?
我們想要的名字混淆一些情況,並且可以決定使用using
或define
,例如:應該用優於定義名稱混淆的C++
using ComponentRef = std::shared_ptr<Component>;
#define ComponentRef std::shared_ptr<Component>
是有一些原因,其中之一是首選在另一個?
總是更喜歡在宏之前使用任何東西。宏代碼可能會以意想不到的方式混淆您的代碼,而當您發現宏代替時,您並不期望它發生。
你應該更喜歡using
,因爲#define
是一個直接的文本替換,它後面根本沒有「智能」。 #define
也會持續存在於您聲明的範圍之外,並可能產生奇怪的不良副作用。
「使用」更好。如果#define似乎有必要,請考慮是否使用「typedef」代替:
typedef std::shared_ptr<Component> ComponentRef;
我沒有downvote。我猜他們downvoted因爲使用通常比typedef這些日子更好。雖然這看起來並不像你說的那樣。 –
我會克服它的。 –
'#define' <'typdef' <'using'。 – Jarod42