Problem I was just trying to debug a set of file-manipulation routines I wrote f
ID: 654675 • Letter: P
Question
Problem
I was just trying to debug a set of file-manipulation routines I wrote for a program I am working on. One of them kept returning an INVALID_HANDLE error.
Explanation
I figured out what the problem was. It turns out that because I provided several overloads so that I could call the function with either a file handle or filename, I ended up creating ambiguous overloads.
For example (not variadic functions; the ellipses are just for simplification purposes):
INT CopyFileSection ( HANDLE fIn, HANDLE fOut,
Explanation / Answer
The easiest way to limit the combinatorial explosion of your overloads is to introduce a small helper class that can accept all the various types and convert them to a single common type.
For example
class FileHelper {
public:
FileHelper(HANDLE& h) : handle(h) {}
FileHelper(const TCHAR* fName) : handle(openFile(fName) {}
FileHelper(const std::string& fName) : handle(openFile(fName) {}
HANDLE getHandle() const { return handle; }
private:
HANDLE handle;
}
INT CopyFileSection ( FileHelper fIn, FileHelper fOut,
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.