# Help me understand this answer

This topic is 4248 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I recently finished my map editor and have been trying to actually get the maps loaded in the game. I noticed that the LoadMap function wasn't getting the right values from the dat file. Apparently it did this because of a refrence issue.
void LoadMap(MAP& map) //putting the amperstand in made the problem go away
{
//Load matrix from file

std::ifstream file(map.fileName, std::ios::in | std::ios::binary);
if(file.is_open())
{
file.close();
}
}

Now, if anyone could explain to me why this happens I would greatly appreciate it.

##### Share on other sites
The ampersand means that you're passing a reference to the map, not a copy. If you remove the ampersand a copy of the map is made and it's destroyed at the end of the function, this means that everything you read into the map is only read into this new temporary map, not the original one. When you use a reference you will use the original map, so the map is never copied you are reading into the original map.

##### Share on other sites
The ampersand is the reference operator. Without it, the parameter is copied by the function. When you modify it inside the function, only the copy gets modified, thus leaving the original parameter intact. The reference operator causes the original parameter to be modified since it doesn't make an internal copy of it.

EDIT: CTar got it first :)

##### Share on other sites
I think it works with the ampersand because you are passing a reference to a MAP whereas without the ampersand you were passing by value, essentially creating a copy of the MAP passed in as an argument. Its possible that when you passed by value (without the ampersand) that the new map created didnt properly copy the map you passed in as an argument.

So:
1. With ampersand you passed a reference to the MAP coming in as an arugment.
2. Without ampersand you passed a copy of the MAP coming in as an argument.

##### Share on other sites
Thanks, that makes perfect sense.