I'm trying to learn C++ again, trying to do all the modern stuff (e.g., avoid pointers in favor of references) and I found that I have some trouble understanding how references and lifetimes work. I'm using clang with -std=c++11.I want to have a RAII-class, RoomList that basically wraps a map<int, Room>. On construction of RoomList, it will create all the Rooms (imagine loading them from a data file or so) and the map, and on destruction it should free the map and all Rooms.This code works, but I don't fully understand all of it (I've done a lot of randomly trying stuff to get it to work). (GIST is here)The Room class:class Room {public:  explicit Room(int id, const std::string& name) : m_id(id), m_name(name) {}  const std::string& GetName() const { return m_name; }  const int& GetEnterCount() const { return m_enterCount; };  void OnEnter() { ++m_enterCount; }private:  std::string m_name; // should this be const?  int m_id; // should this be const?  int m_enterCount = 0;};The RoomList class:class RoomList {public:  RoomList();  ~RoomList() = default; // Is this needed? Rule of three?  RoomList(const RoomList&) = delete; // Don't want copies  RoomList& operator=(RoomList const&) = delete;  const Room& GetRoom(int index);private:  // Should this be <int, Room> or <int, Room&>?  std::map<int, Room> m_rooms;};RoomList::RoomList() {  // Read all the rooms, e.g., from a data file  // What is the lifetime of the Room I'm creating?  // Room doesn't have copy or move ctors  // Also, I'm not initializing m_rooms - what is its lifetime?  m_rooms.emplace(0, Room(0, Start Room));}// Returning a reference to const Room - no one should be able to modify Room, except by calling room.OnEnter()// room.OnEnter() should modify the Room inside the map (Individual Rooms are basically Singletons)const Room& RoomList::GetRoom(int index) {  auto result = m_rooms.find(index);  if(result != m_rooms.end())  {    // result is a local var - is result->second valid after return?    return result->second;  }  // Considering Optional<Room>  // https://github.com/akrzemi1/Optional/  throw Invalid Index;}main.cpp:  int main() {  RoomList rooms;  auto room = rooms.GetRoom(0);  std::cout << Name:  << room.GetName() << std::endl;  return 0;}I left some comments, but basically the things I'm confused about:I'm not initializing the m_rooms map - will it be default initialized to live as long as the rooms variable in main.cpp is in scope?I have a map of Rooms, but I want to return Room references. Will returning a Room have any negative side effects? (e.g., is there some automatic creation of a wrapper object or even a copy of Room going on?)Is result->second even still valid after a return? result is a local variable that should die when the function ends, but I'm returning what second points to, so that should be good?Would I need to implement ~RoomList to make sure all the Rooms are freed? Or does std::map::emplace own the constructed Room?If I wanted to return something by value, how would I need to allocate it? For example, imagine I've added this to Room, wouldn't I return a stack-allocated variable?This code works, but I think it shouldn't:class Room {public:  // ...  std::string GetRoomDescription();}std::string Room::GetRoomDescription(){  // What's the lifetime of result?  // In C, this would be stack-allocated and dead  // when the function returns.  std::stringstream result;  result << ID:  << m_id <<  - Name:  << m_name;  return result.str();}// main.cppstd::cout << Room:  << room.GetRoomDescription() << std::endl;
Creating a searchable collection of rooms
c++;c++11