Last active
September 18, 2017 19:35
-
-
Save JPGygax68/5928405 to your computer and use it in GitHub Desktop.
#RIIA wrapper for FILE*. No copy semantics yet.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class FilePtr { | |
public: | |
FilePtr() { fp = nullptr; } | |
FilePtr(FILE *fp_) { fp = fp_; } | |
FilePtr & operator = (FILE *fp_) { assert(fp == nullptr); fp = fp_; return *this; } | |
~FilePtr() { if (fp != nullptr) fclose(fp); } | |
operator FILE * () { return fp; } | |
private: | |
FILE *fp; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment