我是撰寫運算子的新手(在這種情況下是==and !=)。我做了一些研究,到目前為止想出了:
bool operator==(const SPECIAL_EVENT_S &rsEvent)
{
bool bSame = false;
if (rsEvent.datEvent == m_datSpecialEvent &&
rsEvent.strEvent == m_strNotes &&
rsEvent.strLocation == m_strLocation &&
rsEvent.datEventStartTime == m_datEventStartTime &&
rsEvent.datEventFinishTime == m_datEventFinishTime &&
gsl::narrow<bool>(rsEvent.bEventAllDay) == m_bEventAllDay &&
gsl::narrow<bool>(rsEvent.bSetReminder) == m_bSetReminder &&
rsEvent.iReminderUnitType == m_iReminderUnitType &&
rsEvent.iReminderInterval == m_iReminderInterval &&
rsEvent.iImageWidthPercent == m_wImageWidthPercent &&
rsEvent.strImagePath == m_strImagePath &&
rsEvent.strTextBeforeImage == m_strTextBeforeImage &&
rsEvent.strTextAfterImage == m_strTextAfterImage &&
rsEvent.eType == m_eVideoconfType &&
rsEvent.sBSSTI == m_sBSSTI)
{
// The fundamental information is unchanged
bSame = true;
}
// Now compare the MWB Event Type
if (bSame)
{
switch (rsEvent.eMWBEventType)
{
case EventTypeMWB::MWBBethelSpeakerServiceTalk:
return m_bSpecialEventBethelServiceTalk;
case EventTypeMWB::MWBVideoconferenceAssembly:
return m_bSpecialEventVideoconf && m_eVideoconfType == VideoConferenceEventType::Live;
case EventTypeMWB::MWBVideoconferenceConvention:
return m_bSpecialEventVideoconf && m_eVideoconfType == VideoConferenceEventType::Recorded;
case EventTypeMWB::MWBSpecialEvent:
return m_bSpecialEvent;
case EventTypeMWB::MWBMemorial:
return m_bEventMemorial;
case EventTypeMWB::MWBCircuitOverseerMeeting:
return m_bCircuitVisit || m_bCircuitVisitGroup;
case EventTypeMWB::MWBMeeting:
return !m_bNoMeeting;
default:
bSame = false;
}
}
return bSame;
}
bool operator!=(const SPECIAL_EVENT_S& rsEvent)
{
return !(rsEvent == *this);
}
當我嘗試使用這些運算子時,讓我感到驚訝的是:
if (pEntry != sEvent)
{
AfxMessageBox(_T("The special event information has changed"));
}
它不喜歡pEntry成為指標。最后我這樣做了:
if (*pEntry != sEvent)
{
AfxMessageBox(_T("The special event information has changed"));
}
- 為什么這是一個問題?我這樣問是因為如果這是一個標準函式,那么物件是否是指標都沒有關系。
- 滿足這種情況的正確方法是什么?
例如:
object->Function(value)object.Function(value)
Function當它是 / 不是指標時,物件可以同時使用它。那么為什么不使用operator?
uj5u.com熱心網友回復:
Function當它是 / 不是指標時,物件可以同時使用它。
實際上,不,它不能。在像(成員訪問)和(函式呼叫)這樣object->Function(value)的陳述句/運算式中,運算子具有相同的優先級和從左到右的關聯性。因此,首先應用 并自動取消參考指標。因此,效果與 - 相同,并且仍然在object上呼叫,而不是在指標上呼叫。->()->(*object).Function(value)Function
那么為什么不使用
operator?
呼叫operator函式的語法是(或可以是)相當不同:因為它被定義為運算子,所以您可以使用運算子標記(在兩個運算元之間)而不是使用顯式函式呼叫來呼叫它。但是,您必須傳遞物件,因為這就是運算元的定義。
但是,如果您真的想要,您仍然可以使用顯式函式呼叫語法來呼叫運算子覆寫;并且,在這種情況下,您可以使用->on 指標;像這樣(operator==實際上函式的“名稱”在哪里):
if (!pEntry->operator==(sEvent))
{
AfxMessageBox(_T("The special event information has changed"));
}
但是,這似乎是一項艱巨的作業,而您*pEntry != sEvent實際上是使用覆寫的“正確”方式。
PS:作為獎勵,如果您使用的是支持 C 20(或更高版本)標準的編譯器,您可以在結構/類中添加“默認”operator==,這樣可以節省您顯式比較每個單獨的資料成員:
struct foo {
int a;
double b;
bool operator==(const foo&) const = default; // Compares "a" and "b"
};
struct bar {
foo f;
int c;
int d;
bool operator==(const bar&) const = default; // Compares "c", "d" and "f"
};
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/425191.html
