I am trying to have a function return a boolean and an address in memory.
I am loading a file from an archive and would like it to return A. if the file was found and B. a pointer to the file in memory after it is loaded. I realize that I cannot have two return types so I have the function as a boolean so it can tell me more importantly if the file exists in the archive. I figured I would do something like this:
// Hold the location of the file
char* location = NULL;
if(!archive.getFile(name, location))
{
errorLog.writeError("Could not find the file!");
return false;
}
If the file is not found, the function that calls the getFile will return false indicating the file was not found. That works great. But I am trying to figure out how to directly modify the pointer ‘location’ from within the getFile function.
Do I pass in the pointer? Or do I pass in the address of the pointer and modify it?
Here is the function in my header:
bool archive::getFile(string filename, char& location)
So to recap, I want to pass in the ‘location’ pointer, have it point to the file in the archive from getFile function and then when it returns, be able to use the ‘location’ pointer to access the file.
Thank you.
Just return a pointer, and a null pointer if the thing is not found.
Then you can do:
But that looks like C code.
Alternatively:
std::stringin the error caseOr indeed pass a pointer-to-pointer or a reference-to-pointer, but that’s the least “idiomatic” C++ you could do IMO.