How do I pass a reference to a class object from a range based for loop to a function argument?

Viewed 10

I have my parser checking through a user input string, and comparing each individual word to an item or room name. These items and rooms are stored as class objects in a vector. I want to be sure that what I am passing to my functions is the original, rather than a copy. Here's what I have:

void parser::parse(string input,character& player) {
    room location;
    item obj1;
    item obj2;
    split(input, out);
    for (const auto &i : out) {
        for (auto j : verbs) {
            if (i == j) {
                verb = j;
            }
        }
        for (auto &l : rooms){
            if (i == l.getName())
                location = l;
        }
        for (auto &k :items) {
            if (i == k.getName()) {
                if (objectOne == "null 1") {
                    obj1 = k;
                    objectOne = i;
                }
                else obj2 = k;
            }
        }

    }
    if (verb == "null V") {
        goofedIt();
    }
    
    if (verb == "go")
        go(location,player);
    if (verb == "get")
          get(obj1,player);
    if (verb == "help")
          help();
    //  if (verb == "use") {
    //      if (objectTwo == "null 2")
    //          use(objectOne);
   //       else use(objectOne, objectTwo);
   //   }
   if (verb == "inspect"){
        if (objectOne == "null 1")
            inspectRoom(location, player);
        else
            inspectItem(obj1,player);
    }

    if (verb == "quit")
        quit();
   // if (verb == "inventory")
   //     inventory();

}

What I have is working fine for things like "Inspect," but for a function like "Get," I need to be sure I'm changing the location of the original item rather than a copy of it.

0 Answers
Related