邏輯 AND 和 OR 運算子

這些運算子在 C++中具有通常的優先順序:OR 之前的 AND。

// You can drive with a foreign license for up to 60 days
bool can_drive = has_domestic_license || has_foreign_license && num_days <= 60;

此程式碼等效於以下內容:

// You can drive with a foreign license for up to 60 days
bool can_drive = has_domestic_license || (has_foreign_license && num_days <= 60);

新增括號不會改變行為,但它確實使它更容易閱讀。通過新增這些括號,不存在關於作者意圖的混淆。