I have decided to revisit this question (Is it possble to return a reference to a std::map?) because it is not correct for me.
My application class has these methods to retreive a map:
SpecialEventDataMap& GetEventsListMap() noexcept
{
return m_mapSpecialEvents;
}
const SpecialEventDataMap& GetEventsListMap() const noexcept
{
return m_mapSpecialEvents;
}
The variable m_mapSpecialEvents
is defined:
using SpecialEventDataMap = std::map<CString, SPECIAL_EVENT_S>;
My main app build a map of special events. This is retreived in my month / calendar control class:
CMyMonthCalCtrl::CMyMonthCalCtrl() noexcept(false)
: m_pReminders(nullptr)
{
m_mapSpecialEvents = theApp.GetEventsListMap();
}
In my application dialog, which has the calendar control, we display a popup dialog where the user can add events. When that dialog returns, we save the content to disk:
theApp.SaveEventsList();
In the end I had to get the main application class to update it's own map:
theApp.AddOrUpdateEventInList(sEvent);
Why did I have to do this? I have since noticed that I also have to call a new function in the calendar class to:
m_calStart.RefreshMap();
m_calEnd.RefreshMap();
Why did my original attempt to get a reference to the map in the app class not work? It clearly made a copy/ I know this because my calendar displays a tooltip for all special events. And unless I restarted the app the tips did not show. When I refreshed the map so that it was the latest instance it worked.
I understood that this code shoudl return a reference. And that I shoudl be able to add / remove from it inside the class that retreieved it, and that the main app class where the original is should be in sync. But not.